回调函数JSONP与jQuery阿贾克斯 [英] Callback function for JSONP with JQuery ajax

查看:126
本文介绍了回调函数JSONP与jQuery阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白如何与回调的jQuery的AJAX funcion工作。

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

我有以下的code。在JavaScript的:

I have the following code in the 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();
    }

和服务器端(AppEngine上/ Python)的我得到的回调参数的值,并与回应

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

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

但后来我得到一个错误: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.

推荐答案

这是我做我的。

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

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

这篇关于回调函数JSONP与jQuery阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆