简体   繁体   English

执行自己的JSONP函数

[英]Execute your own JSONP function

I am using JSONP , and my url(action) is returning me function called processTemplates. 我正在使用JSONP,并且我的url(action)返回了名为processTemplates的函数。 Is there any way I can execute my own function instead of processTemplates. 有什么方法可以执行我自己的功能而不是processTemplates。

Something Like _processJSONCallBack _processJSONCallBack之类的东西

    function _processOverideCallBack(actionName) {
         $.ajax({
                type: 'GET',
                url: actionName,
                async: false,
                contentType: "application/javascript",
                jsonpCallback: '_processJSONCallBack',
                dataType: "jsonp",
                error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    alert('Not connect.\n Verify Network.');
                } else if (jqXHR.status == 404) {
                    alert('Requested page not found. [404]');
                } else if (jqXHR.status == 500) {
                    alert('Internal Server Error [500].');
                } else if (exception === 'parsererror') {
                   alert('Requested JSON parse failed.');
                } else if (exception === 'timeout') {
                    alert('Time out error.');
                } else if (exception === 'abort') {
                    alert('Ajax request aborted.');
                } else {
                    alert('Uncaught Error.\n' + jqXHR.responseText);
                }
            }
        });
};

But when I am trying to do its throwing an exception "Requested JSON parse failed." 但是,当我尝试执行此操作时,将引发异常“请求的JSON解析失败”。

Please help me on this. 请帮我。

From the jQuery docs : jQuery文档

Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation 跨域请求和dataType:“ jsonp”请求不支持同步操作

You are probably getting an error because you have async: false . 您可能会收到错误消息,因为您有async: false

You can get called with the data from a JSONP Ajax call in a much simpler fashion than you're trying to do because jQuery will do all the work for you of creating a callback function. 与JSONP Ajax调用相比,您可以以比您尝试的方式简单得多的方式调用数据,因为jQuery会为您完成创建回调函数的所有工作。 You can just do this by using a success handler: 您可以使用success处理程序来做到这一点:

function _processOverideCallBack(actionName) {
     $.ajax({
            type: 'GET',
            url: actionName,
            dataType: "jsonp",
            success: function(data) {
                // data will be your already parsed JSONP data here
            },
            error: function(jqXHR, exception) {
                    if (jqXHR.status === 0) {
                    alert('Not connect.\n Verify Network.');
                } else if (jqXHR.status == 404) {
                    alert('Requested page not found. [404]');
                } else if (jqXHR.status == 500) {
                    alert('Internal Server Error [500].');
                } else if (exception === 'parsererror') {
                   alert('Requested JSON parse failed.');
                } else if (exception === 'timeout') {
                    alert('Time out error.');
                } else if (exception === 'abort') {
                    alert('Ajax request aborted.');
                } else {
                    alert('Uncaught Error.\n' + jqXHR.responseText);
                }
            }
    });
};

Also, you cannot use async: false with JSONP. 另外,您不能对JSONP使用async: false That is not supported due to how JSONP requests work. 由于JSONP请求的工作方式,因此不支持该功能。 All JSONP requests must be async. 所有JSONP请求都必须是异步的。

In addition, please remember that JSONP is a different type of request than a normal Ajax request and your server MUST support the JSONP type request. 另外,请记住,JSONP是与普通Ajax请求不同的请求类型,并且您的服务器必须支持JSONP类型请求。 You can't make a JSONP request of a normal Ajax URL. 您无法发出普通Ajax URL的JSONP请求。 The server must produce a JSONP formatted response. 服务器必须产生JSONP格式的响应。

jsonpCallback is the parameter name of the callback function when the request is built. jsonpCallback是构建请求时回调函数的参数名称 Typically a JSONP request looks like this: 通常,JSONP请求如下所示:

http://example.com/endpoint ? http://example.com/endpoint callback =yourfunction&foo=bar 回调 = yourfunction&foo = bar

Based on your code, jQuery is probably generating something like this: 根据您的代码,jQuery可能会生成以下内容:

http://example.com/endpoint ? http://example.com/endpoint _processJSONCallBack =yourfunction&foo=bar _processJSONCallBack =您的功能&foo = bar

Whatever API you're using doesn't know the name of your callback function because it isn't being set in the right parameter. 您正在使用的任何API都不知道您的回调函数的名称,因为没有在正确的参数中进行设置。 Check the documentation to see what the callback parameter name should be. 检查文档以查看回调参数名称应为什么。 jQuery defaults to 'callback' if jsonpCallback isn't specified. 如果未指定jsonpCallback,则jQuery默认为“回调”。

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

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