0

我正在调用一个 javascript 函数,该函数在按钮的 onclick 上进行 ajax 调用,代码是:

<button onclick="getTextValue()" id="pass_text_values" >Submit</button>

Javascript函数是:

function getTextValue() 
{
  var user_postcode = document.getElementById('postcode').value;
  alert("postcode value entered by user = "+user_postcode);

  var brand_id = document.getElementById('getBrand').value;
  alert("entered value of brand = "+brand_id);

  var product_type_id = document.getElementById('getProductType').value;
  alert("entered value of brand = "+product_type_id);

  remoteCall(user_postcode, brand_id, product_type_id); 
  //document.getElementById("pass_text_values").onclick =  remoteCall('m14', 1, 1);

}//end of func to get textfield value().

remotecall 函数对服务器进行 ajax 调用并显示数据,代码为:

function remoteCall(postcode, brand_id, product_type_id)
{


try
{

  alert("values Inside TRY:\n Postcode = "+postcode+"\n Brand = "+brand_id+"\n                 Product type = "+product_type_id);
  var serverUrl = "http://192.168.1.200/kruthika/findenggapi.php?postcode_s="+postcode+"&brand_id="+brand_id+"&product_type_id="+product_type_id;
  alert("URL : \n "+serverUrl);

  $.ajax({
    type: 'GET',
    url: serverUrl ,
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: function(data)
    {
        if(data == '')
          alert("No data received from server");
        else
        { 
            for (var i = 0; i < data.length; i++) 
            { 
                var disp_data = "name = "+data[i].name+"<br> town = "+data[i].town+"<br> phone = "+data[i].phone+"<br> mobile = "+data[i].mobile+"<br> Email = "+data[i].email+"<hr>";
                alert("Data from server = "+disp_data);

            }

        }//end of else.
    },//ens od success.        
    error: function(xhr, textStatus, errorThrown)
    {
        alert(xhr.responseText);
        alert('FAIL !!!');
    },

});//end of AJAX.


}//end of try
catch(e)
{
    alert("error = "+e.message); 
}

}//END OF FUNC remoteCall.

如果我在外面调用相同的 remotecall 函数,那么它可以工作,但如果我用 onclick 调用它,它就不起作用。任何人都可以帮忙。

4

1 回答 1

0

尝试添加选项

async:false

给你ajax调用..它可能会工作。

于 2013-05-16T13:19:16.390 回答