一点背景:
我正在尝试实现 AJAX 驱动的 SlickGrid。没有太多文档,所以我使用这个例子作为基础。
在此示例中,有以下代码访问所需的 Web 服务以获取数据:
req = $.jsonp({
url: url,
callbackParameter: "callback",
cache: true, // Digg doesn't accept the autogenerated cachebuster param
success: onSuccess,
error: function(){
onError(fromPage, toPage)
}
});
req.fromPage = fromPage;
req.toPage = toPage;
我不确定 jsonp 做了什么,但从我读过的内容来看,它似乎与 jQuery 中的 ajax 方法非常相似,只是它返回 json 并允许跨域请求。我碰巧调用的 web 服务只返回 XML,所以我将这段代码更改为:
req = $.ajax({
url: "/_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: xmlData,
complete: onSuccess,
error: function (xhr, ajaxOptions, thrownError) {
alert("error: " + xhr.statusText);
alert(thrownError);
},
contentType: "text/xml; charset=\"utf-8\""
});
req.fromPage = fromPage;
req.toPage = toPage;
我的问题是我的页面错误,req.fromPage = fromPage;
因为req
是空的。
我认为我可以用对 ajax 方法的调用替换我的 jsonp 调用是错误的吗?req 只是没有设置,因为我的 ajax 调用在代码执行时还没有完成?我怎样才能解决这些问题?
如果我注释掉最后两行并将这些值硬编码到其他地方,一切都会运行良好。