【问题标题】:Ajax Post Content Type : Application / Json Block the requestAjax Post Content Type : Application / Json Block the request
【发布时间】:2013-04-15 00:58:06
【问题描述】:

这是我的 Ajax 请求:

$.ajax({ 网址:'', 类型:'POST', 数据:JSON.stringify({国家:jcountry,地区:jregion,来自:jfrom,to:jto,货币:jcurrency}), 处理数据:假, 内容类型:'应用程序/json', 数据类型:'json', 成功:函数(){ 警报(“成功”) $.mobile.changePage("menu1.html"); }, 错误:函数(xhr,ajaxOptions,throwError){ alert("错误:" + xhr.status + "\n" + "消息:" + xhr.statusText + "\n" + "响应:" + xhr.responseText + "\n" + throwedError); $.mobile.changePage("menue2.html"); } });

如果我不精确的内容类型,我将无法再看到我对 firebug 的请求。 相反,当我添加内容类型时,我可以看到 POST 请求(错误导致我的 URL 为 false),但在我的标头中,内容类型默认为 url 编码形式。

我想要的是通过 JSON 数据发送表单的详细信息。感谢您的帮助

【问题讨论】:

    标签: jquery ajax json post content-type


    【解决方案1】:

    您将 contentType 拼错为 content-Type

    $.ajax({
        url: '',
        type: 'POST',
        data: JSON.stringify({
            country: jcountry,
            region: jregion,
            from: jfrom,
            to: jto,
            currency: jcurrency
        }),
        processData: false,
        ContentType: 'application/json',
        dataType: 'json',
        success: function () {
            alert("success")
            $.mobile.changePage("menu1.html");
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Error: " + xhr.status + "\n" +
                "Message: " + xhr.statusText + "\n" +
                "Response: " + xhr.responseText + "\n" + thrownError);
            $.mobile.changePage("menue2.html");
        }
    });
    

    【讨论】: