我正在调用一个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调用它,它就无法正常工作。任何人都可以提供帮助。
答案 0 :(得分:0)
尝试添加选项
async:false
给你ajax电话..那可能会有用。