0

我的情况很奇怪。我有一个 .NET Web 服务 (.asmx),我试图通过 jQuery ajax 调用它来填充一些<select></select>'s。该服务正在按预期运行System.Web.Services,并且应该返回一个 JSON 列表。至少这是我理解它在 .NET 3.5/4.0 中的方式。

主要问题是.ajax()函数(如下)似乎执行但内部回调没有执行。我知道它正在执行,因为我可以在函数之前和之后做一些事情并且都执行,所以 JavaScript 不会爆炸。

    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: 'http://localhost:59191/AFEDropDownService/Service.asmx/GetAVP',
    data: "{}",
    dataType: "jsonp",
    crossDomain: true,
    success: function(list) {
        console.log(list);
        console.log("success!!");
    },
    error: function(msg) {
        console.log("error: " + msg.text);
    }
});

如果您需要更多代码,请告诉我。或者,如果您在我尝试构建 cascading 时知道更好<select></select的方法。

更新

我将它从“jsonp”更改为“json”,现在我回到了“未定义”的错误消息。有没有办法找出未定义的内容?恐怕我有点过头了。

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: 'http://localhost:59191/AFEDropDownService/Service.asmx/GetAVP',
    data: "{}",
    dataType: "json",
    success: function(list) {
        console.log(list);
        console.log("success!!");
    },
    error: function(msg) {
        //console.log("error: " + msg.text);
    }
});

更新 2

我输入了以下.ajaxError()代码,它在萤火虫中为“thrownError”对象显示了一个“空字符串”。

$("#sel_AFE").ajaxError(function(e,xhr,settings,thrownError){
    console.log('error in: ' + settings.url + ' \n'+'error:\n' + thrownError);
});
4

2 回答 2

0

jQuery 文档说:

如果带有 jQ​​uery.post() 的请求返回错误代码,它将静默失败,除非脚本还调用了全局 .ajaxError() 方法。或者,从 jQuery 1.5 开始,jQuery.post() 返回的 jqXHR 对象的 .error() 方法也可用于错误处理。

这是一个如何使用successerror回调的示例:

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post("example.php", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });
于 2012-09-13T06:40:35.120 回答
0

你为什么不只使用json而不是jsonp ..还有你为什么设置跨域..

你在浏览器的响应部分看到你的 jsonp 数据了吗??

也使用代替

error: function(msg){
       console.log("error: " + msg.text);
 } .. try Using 

error: function(jqXHR, textStatus, errorThrown){
  // log all the details here
} 
于 2012-09-12T21:36:28.830 回答