简体   繁体   English

没有调用jQuery.ajax转换器

[英]jQuery.ajax converter not called

I'm having trouble with jQuery.ajax converters - I can't get my converter to be called. 我遇到了jQuery.ajax转换器的问题 - 我无法调用我的转换器。

I've got this jQuery AJAX code (simplified for the question) : 我有这个jQuery AJAX代码(简化问题):

    $.ajax({
    url: "http://myurl/myservice",
    dataType: "JSONP",
    cache: false,
    success: function (data, textStatus, jqXHR) { /* do stuff */ },
    error: function (jqXHR, textStatus, errorThrown) { /* do stuff */ },
    timeout: 5000,
    converters: { "JSONP": myConversionFunction }
});

When I use this code, the converter function myConversionFunction isn't being called. 当我使用这段代码时,没有调用转换器函数myConversionFunction I want to use the converter to convert dates in the response as show in other SO questions but just can't get it firing. 我想使用转换器来转换响应中的日期,如同在其他SO问题中显示但是无法解决它。

Using fiddler I've checked the response and it is JSONP, with content type "application/x-javascript". 使用fiddler我已经检查了响应,它是JSONP,内容类型为“application / x-javascript”。

Any ideas what I'm doing wrong? 我有什么想法我做错了吗?

Thanks, Chris. 谢谢,克里斯。

I think you can't overwrite jQuery's default converters like json . 我认为你不能覆盖像json这样的jQuery默认转换器。 Introduce your own converter instead (and include text in your specifier, as in this case it's a conversion from text to your output): 引入您自己的转换器(并在您的说明符中包含text ,因为在这种情况下,它是从文本到输出的转换):

$.ajax({
    url: "http://myurl/myservice",
    dataType: "jsonp myConversion",
    cache: false,
    success: function (data, textStatus, jqXHR) { /* do stuff */ },
    error: function (jqXHR, textStatus, errorThrown) { /* do stuff */ },
    timeout: 5000,
    converters: {
        "text myConversion": function(value) {
            console.log("pre-processing...");
            /* do stuff */
            return value;
        }
    }
});

I use code like this to manage the 'd' data of asp.net: 我使用这样的代码来管理asp.net的'd'数据:

$.ajaxSetup({
    data: "{}",
    dataType: "jsonp",
    type: "POST",
    contentType: "application/json",
    converters:
        {
            "json jsonp": function(msg)
            {
                return msg.hasOwnProperty('d') ? msg.d : msg;
            }
        },
    error: function(xhr, textStatus, errorThrown)
    {
        var errorMessage = "Ajax error: " + this.url + " : " + textStatus + " : " + errorThrown + " : " + xhr.statusText +
                " : " + xhr.status;
        if (xhr.status != "0" || errorThrown != "abort")
        {
            alert(errorMessage);
        }
    }
});

perhaps you need to make it lower case like: 也许你需要把它做成小写,如:

converters:
    {
        "json jsonp": function(msg)
        {
            return yourfunction(msg);
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM