AJAX responseText未定义 [英] AJAX responseText undefined
问题描述
JavaScript代码:
Javascript code:
...............
...............
var cutid = $(th).attr("data-cutid");
var request = $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Services/Cut.asmx/CheckCuts",
data: "{'cuts':" + JSON.stringify(ListCuts) + ",'idCut':'" + cutid + "'}",
dataType: "json"
}).responseText;
alert(request); // undefined
网络服务的功能:
[WebMethod]
public string CheckCuts(List<CutM> cuts, Guid idCut)
{
return UtilCut.CheckCuts(cuts, idCut).ToString();
}
responseText未定义.为什么?
The responseText is undefined. Why?
我在ajax请求中添加了 async:false . 将async设置为false意味着您要调用的语句必须先完成,然后才能调用函数中的下一条语句.
I added async: false to ajax request. Setting async to false means that the statement you are calling has to complete before the next statement in your function can be called.
此代码有效:
function AjaxCheckCuts(ListCuts,cutid)
{
var request = $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Services/Cut.asmx/CheckCuts",
async: false,
data: "{'cuts':" + JSON.stringify(ListCuts) + ",'idCut':'" + cutid + "'}",
dataType: "json"
}).responseText;
var r = jQuery.parseJSON(request);
r = r.d;
return r;
}
推荐答案
Web服务是否正常运行?返回HTTP 200吗?您能看到使用F12工具或Fiddler返回的数据吗?
Is the web service working correctly? Is it returning a HTTP 200? Can you see the data that is being returned using F12 tools or Fiddler?
$.ajax()
返回延迟.定义一个done
方法,使其在异步调用完成时执行.没有responseText
属性,这就是为什么它返回undefined
的原因.
$.ajax()
is returning a deferred. Define a done
method for it to execute when the async call completes. There is no responseText
property, that's why it is returning a undefined
.
尝试一下:
var cutid = $(th).attr("data-cutid");
var request = $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Services/Cut.asmx/CheckCuts",
data: "{'cuts':" + JSON.stringify(ListCuts) + ",'idCut':'" + cutid + "'}",
dataType: "json"
});
request.done(function(result){
alert(result);
});
这篇关于AJAX responseText未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!