【问题标题】:Jquery ignore error and success json codeJquery忽略错误和成功json代码
【发布时间】:2013-03-11 15:13:13
【问题描述】:

我尝试使用 phonegap 和 android 使用服务。

我使用 localhost 的服务返回带有 chrome 的 json:

{
    "GetListaMunicipiosResult": [{
        "MunicipioID": "1",
        "MunicipioNome": "Florianópolis",
        "MunicipioUf":"SC"
    }, {
        "MunicipioID": "2",
        "MunicipioNome": "Jaraguá do Sul",
        "MunicipioUf": "SC"
    }]
}

在我的.js 文件中,我使用代码调用 GET json:

$('#cidades_page').live('pageshow',function(event){
    $.ajax("http://10.0.2.2:56976/MunicipiosService.svc/ListaMunicipios",{
        beforeSend: function (xhr) {
        $.mobile.showPageLoadingMsg();
        alert("beforeSend");
        },

        complete: function () {
            // $.mobile.hidePageLoadingMsg();
            alert("complete");
        },
        contentType: "application/json",
        dataType: "jsonp",
        jsonp: "callback",
        type: "GET",
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(xhr.responseText);
            //alert(thrownError);
        },
        success: function (data) {
            alert(JSON.stringify(data));

        }
    });
});

但是当页面显示时,只有警报 alert("beforeSend") 触发,之后什么也没发生。

我使用 $.ajax(.... 在 html 中插入 json 调用,并使用 chrome 及其工作打开。我不知道还能做什么。

感谢帮助

编辑 我在 windows phone 中测试,现在我可以得到错误 Error:GetListaMunicipios was not called.

我的 .js:

$.ajax("http://localhost:56976/MunicipiosService.svc/ListaMunicipios?callback=?",{
        beforeSend: function (xhr) {
            // $.mobile.showPageLoadingMsg();
        alert('beforeSend');
        },

        complete: function () {
            // $.mobile.hidePageLoadingMsg();
            alert('complete');
        },
        contentType: 'application/json; charset=utf-8',
        dataType: 'jsonp',
        crossDomain: true,
        jsonp: 'callback',
        jsonpCallback:'GetListaMunicipios',
        type: 'GET',
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(xhr.responseText);
            alert(thrownError);
        },
        success: function (data) {
            alert('success');

        }
    });

我的 WCF 服务

命名空间 GuiaService {

[ServiceContract]

public interface IMunicipiosService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "ListaMunicipios")]

    List<ClsListaMunicipios> GetListaMunicipios();
}

}

感谢您的帮助。

【问题讨论】:

标签: android jquery json cordova


【解决方案1】:

您应该使用donefailalways

successerrorcomplete 已弃用。

更新:如 cmets 中所述,这不适用于您的使用方式。我现在认为问题在于您使用jsonp 作为类型而不是json

jsonp 设计为在加载时自动调用函数,因此您的服务器应该在生成的代码中添加函数调用。有可能 jQuery 期望这种行为并禁用它自己的回调,或者使用jsonp 机制来触发它自己的回调,但是由于您的服务器实际上并没有添加函数调用,所以什么也没有发生。

【讨论】:

  • 从什么时候开始的?你能提供一个链接吗? (不是怀疑你,只是还没见过)
  • @Steve 在 1.8 中已弃用,如文档中所述:api.jquery.com/jQuery.ajax 可能(可能)在 1.9 / 2.0 中删除
  • 嗯,这表明 jqXHR.success() 方法已被删除,而不是设置对象中的 success 回调。
  • @Steve 确实如此。我一直在对象中设置属性并假设它们在参数中是相同的。我会修正我的答案。
猜你喜欢
  • 2021-07-06
  • 2018-06-21
  • 1970-01-01
  • 2014-04-23
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多