【问题标题】:Ajax : passing value to json by special methodAjax:通过特殊方法将值传递给 json
【发布时间】:2016-05-11 06:00:48
【问题描述】:

我正在开发一个依赖于 nodeJS 的应用程序,我发现我无法以如下标准方式将值传递给 JSON

$(".test").each(function(i, e) {
                var testID = $(e).html();
                $(testID).click(function(){

                    var postData = {};
                    postData.profileId = "120";
                    postData.region = "1";
                    postData.postUrl ="www.google.com";

                    var api_submit = "/buzz/view/data/editRegion"; //API
                    $.post(api_submit, postData).done(function (data) {

                        });
                    });
                });

它没有给出任何错误消息,但 JSON 根本没有更新。 我在 nodeJs 中做错了吗?

【问题讨论】:

    标签: json ajax node.js


    【解决方案1】:

    尝试使用以下Ajax函数:

    $.ajax({
        type: "POST",
        url: api_submit,
        data: JSON.stringify({ profileId: 1, region: 'xx', postUrl: 'xo' }),
        contentType: "application/json; charset=utf-8", // You must specific this!
        dataType: "json", // This is the returned data you'll get, if it's not json there will be an error
        success: function(data){alert(data);},
        failure: function(errMsg) {
            alert(errMsg);
        }
    });
    

    如果您的节点应用程序接受json,则此 ajax 函数将起作用。

    编辑:

    或者,您可以创建对象before ajax 函数

    var postData = {
        profileId: "120",
        region: "1",
        postUrl: "www.google.com",
    };
    

    然后只需更改以下行:

    data: JSON.stringify({ profileId: 1, region: 'xx', postUrl: 'xo' }),
    

    收件人:

    data: JSON.stringify(postData),
    

    【讨论】:

    • 我试过了,但它似乎没有向 json 发送任何内容
    • i tried it, but it seem doesn't send anything to the json 是什么意思。我不确定您在节点服务器上使用什么?你在使用 express.js 吗?如果是这样,请确保您在 npm 上使用 bodyparser 包!
    • 对不起,你是对的,是后端的问题,谢谢,我解决了
    • @anson920520 - 太好了!
    猜你喜欢
    • 2013-09-01
    • 2011-01-23
    • 2018-06-24
    • 2021-08-06
    • 2017-03-02
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多