JSONP的回调函数与JQuery ajax。

时间:2022-12-08 19:39:01

I didn't quite understand how to work with the callback for the ajax funcion of JQuery.

我不太明白如何使用JQuery的ajax funcion回调函数。

I have the following code in the JavaScript:

我在JavaScript中有以下代码:

    try {
        $.ajax({
            url: 'http://url.of.my.server/submit?callback=?',
            cache: false,
            type: 'POST',
            data: $("#survey").serialize(),
            dataType: "jsonp",
            timeout: 200,
            crossDomain: true,
            jsonp: 'jsonp_callback',
            success: function (data, status) {
                mySurvey.closePopup();
            },
            error: function (xOptions, textStatus) {
                mySurvey.closePopup();
            }
        });
    } catch (err) {
        mySurvey.closePopup();
    }

And on the server side (AppEngine / Python) I get the value of the callback parameter and respond with

在服务器端(AppEngine / Python)中,我获取回调参数的值并进行响应。

    self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
    self.response.out.write(callback + '({"msg": "ok"});')

But then I get an "Error: jQuery152042227689944248825_1317400799214 is not a function" on the browser.

但是我得到一个“错误:jQuery152042227689944248825_1317400799214在浏览器上不是一个函数”。

What is the proper way to handle this? Right now I get the results that I need, but the fact that I know it's not right is bothering me.

正确的处理方法是什么?现在我得到了我需要的结果,但我知道这是不对的这一事实困扰着我。

3 个解决方案

#1


30  

This is what I do on mine

这是我的工作。

 $(document).ready(function () {
if ($('#userForm').valid()) {
                var formData = $("#userForm").serializeArray();
                $.ajax({
                    url: 'http://www.example.com/user/' + $('#Id').val() + '?callback=?',
                    type: "GET",
                    data: formData,
                    dataType: "jsonp",
                    jsonpCallback: "localJsonpCallback"
                });
    });

 function localJsonpCallback(json) {
        if (!json.Error) {
            $('#resultForm').submit();
        }
        else {
            $('#loading').hide();
            $('#userForm').show();
            alert(json.Message);
        }
    }

#2


12  

delete this line:

删除这条线:

jsonp: 'jsonp_callback',

Or replace this line:

这条线或更换:

url: 'http://url.of.my.server/submit?callback=json_callback',

because currently you are asking jQuery to create a random callback function name with callback=? and then telling jQuery that you want to use jsonp_callback instead.

因为当前您要求jQuery创建一个带有callback=的随机回调函数名?然后告诉jQuery您想要使用jsonp_callback。

#3


8  

$.ajax({
        url: 'http://url.of.my.server/submit',
        dataType: "jsonp",
        jsonp: 'callback',
        jsonpCallback: 'jsonp_callback'
    });

jsonp is the querystring parameter name that is defined to be acceptable by the server while the jsonpCallback is the javascript function name to be executed at the client.
When you use such url:

jsonp是定义为服务器可接受的querystring参数名称,而jsonpCallback是在客户端执行的javascript函数名。当你使用这样的网址:

url: 'http://url.of.my.server/submit?callback=?'

the question mark ? at the end instructs jQuery to generate a random function while the predfined behavior of the autogenerated function will just invoke the callback -the sucess function in this case- passing the json data as a parameter.

问号?最后,指示jQuery生成一个随机函数,而自动生成函数的预被罚行为只会调用回调函数(在本例中是sucess函数),将json数据作为参数传递。

$.ajax({
        url: 'http://url.of.my.server/submit?callback=?',
        success: function (data, status) {
            mySurvey.closePopup();
        },
        error: function (xOptions, textStatus) {
            mySurvey.closePopup();
        }
    });


The same goes here if you are using $.getJSON with ? placeholder it will generate a random function while the predfined behavior of the autogenerated function will just invoke the callback:

如果你使用$,这里也是一样。getJSON ?占位符将生成一个随机函数,而自动生成函数的预罚行为只会调用回调函数:

$.getJSON('http://url.of.my.server/submit?callback=?',function(data){
//process data here
});

#1


30  

This is what I do on mine

这是我的工作。

 $(document).ready(function () {
if ($('#userForm').valid()) {
                var formData = $("#userForm").serializeArray();
                $.ajax({
                    url: 'http://www.example.com/user/' + $('#Id').val() + '?callback=?',
                    type: "GET",
                    data: formData,
                    dataType: "jsonp",
                    jsonpCallback: "localJsonpCallback"
                });
    });

 function localJsonpCallback(json) {
        if (!json.Error) {
            $('#resultForm').submit();
        }
        else {
            $('#loading').hide();
            $('#userForm').show();
            alert(json.Message);
        }
    }

#2


12  

delete this line:

删除这条线:

jsonp: 'jsonp_callback',

Or replace this line:

这条线或更换:

url: 'http://url.of.my.server/submit?callback=json_callback',

because currently you are asking jQuery to create a random callback function name with callback=? and then telling jQuery that you want to use jsonp_callback instead.

因为当前您要求jQuery创建一个带有callback=的随机回调函数名?然后告诉jQuery您想要使用jsonp_callback。

#3


8  

$.ajax({
        url: 'http://url.of.my.server/submit',
        dataType: "jsonp",
        jsonp: 'callback',
        jsonpCallback: 'jsonp_callback'
    });

jsonp is the querystring parameter name that is defined to be acceptable by the server while the jsonpCallback is the javascript function name to be executed at the client.
When you use such url:

jsonp是定义为服务器可接受的querystring参数名称,而jsonpCallback是在客户端执行的javascript函数名。当你使用这样的网址:

url: 'http://url.of.my.server/submit?callback=?'

the question mark ? at the end instructs jQuery to generate a random function while the predfined behavior of the autogenerated function will just invoke the callback -the sucess function in this case- passing the json data as a parameter.

问号?最后,指示jQuery生成一个随机函数,而自动生成函数的预被罚行为只会调用回调函数(在本例中是sucess函数),将json数据作为参数传递。

$.ajax({
        url: 'http://url.of.my.server/submit?callback=?',
        success: function (data, status) {
            mySurvey.closePopup();
        },
        error: function (xOptions, textStatus) {
            mySurvey.closePopup();
        }
    });


The same goes here if you are using $.getJSON with ? placeholder it will generate a random function while the predfined behavior of the autogenerated function will just invoke the callback:

如果你使用$,这里也是一样。getJSON ?占位符将生成一个随机函数,而自动生成函数的预罚行为只会调用回调函数:

$.getJSON('http://url.of.my.server/submit?callback=?',function(data){
//process data here
});