执行您自己的JSONP函数

Execute your own JSONP function

本文关键字:函数 JSONP 自己的 执行      更新时间:2023-09-26

我正在使用JSONP,我的url(action)正在返回我名为processTemplates的函数。我能不能执行我自己的函数processtemplate .

之类的_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);
                }
            }
        });
};

但是当我试图做它抛出一个异常"请求的JSON解析失败。"

请帮帮我。

来自jQuery文档:

不支持跨域请求和dataType: "jsonp"请求同步操作

你可能会得到一个错误,因为你有async: false

您可以以一种比您尝试做的简单得多的方式从JSONP Ajax调用中获得数据,因为jQuery将为您完成创建回调函数的所有工作。您可以通过使用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);
                }
            }
    });
};

同样,你不能在JSONP中使用async: false。由于JSONP请求的工作方式,这是不支持的。所有JSONP请求必须是异步的。

另外,请记住JSONP是一种不同于普通Ajax请求的请求类型,您的服务器必须支持JSONP类型的请求。您不能对普通Ajax URL发出JSONP请求。服务器必须生成JSONP格式的响应。

jsonpCallback是构建请求时回调函数的参数名。典型的JSONP请求是这样的:

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

根据您的代码,jQuery可能生成如下内容:

http://example.com/endpoint? _processJSONCallBack = yourfunction& foo = bar

你使用的任何API都不知道回调函数的名称,因为它没有被设置在正确的参数中。查看文档以查看回调参数名称。如果没有指定jsonpCallback, jQuery默认为'callback'。