ajax功能
function Verify(ccode,dgh)
{
str = "ccode="+ccode+"&dgh="+dgh;
console.log(str);//this outputs means that this functions gets called
$.ajax({
type: "POST",
url: "ajax/verify",
data: str,
async: false,
cache: false,
error: function (xhr, ajaxOptions, thrownError)
{
console.log(xhr.status);
console.log(thrownError);
},
success: function(json)
{
console.log("in-fun: "+json.code); //does not gets executed
return json.code; //does not return value
},
failure:function(response)
{
console.log("Ajax call failed"); //does not executes
}
});
}
上面的ajax函数被称为var e = Verify(var1, var2);
在ajax请求之后,e
的值未定义。
ajax请求确实命中了我的Web服务器并在apache日志和开发工具中可见,并返回200 OK。 Ajax端点正在工作,并返回一个有效的json。页面输出标题也设置为json
编辑:更新了上面的代码
function Verify(ccode,dgh)
{
var retData = '';
str = "ccode="+ccode+"&dgh="+dgh;
console.log(str); // this works
$.ajax({
type: "POST",
url: "ajax/verify",
data: str,
async: false,
cache: false,
error: function (xhr, ajaxOptions, thrownError)
{
console.log(xhr.status); //does not gets called
console.log(thrownError);
},
success: function(json)
{
console.log("in-fun: "+json.code); //this does not ouputs anything
retData = json.code;
},
complete:function(response)
{
console.log("Complete called"); //does not gets called
}
});
return retData;
}
答案 0 :(得分:2)
由于已经使用了jQuery AJAX来电,您可以依赖以下deferred object:
function Verify(ccode, dgh)
{
var str = "ccode="+ccode+"&dgh="+dgh;
console.log(str); //debug outputs
return $.ajax({
type: "POST",
url: "ajax/verify",
data: str
});
}
Verify(var1, var2).done(function(json) {
if (json) {
var e = json.code;
// more code for the success case
}
else {
console.log("Invalid server response");
}
}).fail(function() {
console.log("Ajax call failed");
});