【问题标题】:How can I insure jQuery submits the correct JSON keys in the headers using POST?如何确保 jQuery 使用 POST 在标头中提交正确的 JSON 键?
【发布时间】:2017-05-29 09:13:05
【问题描述】:

我有这个sn-p,

 pKey = $(this).attr("data-pk");
 var columnName = $(this).attr("name");
 var changedData = $(this).val();
 var data = {
        id : pKey,
        columnName : changedData
    };

    $.post('/HelloWorld/Edit/', data, function () {
        $("#status").html("<strong>" + pKey + ", " + myCurrentData + ": POST SUCCESS?:</strong>");
    })

当我在 Chrome 中单步执行代码时,数据具有正确的列名。应该是这样的:

var data = {
    id: pKey,
    lastName : changedData
};

这就是 Chrome 显示的内容,但是当我看到标题时,它会显示:

columnName : mydatathatgotchanged

换句话说,它保留了列名的字面“columnName”,然后是数据。它正在以某种方式发生变化。 changedData 值是正确的。

【问题讨论】:

  • 您不能将变量用作 Javascript 对象名称,请尝试使用 [] 表示法键入对象并使用您的变量名称。 data[columnName] = changedData
  • 我认为它现在正在工作。我试试看。

标签: jquery json ajax post


【解决方案1】:

这样使用:

...
var data = {id: pKey};
data[columnName] = changedData;
...

【讨论】:

    猜你喜欢
    • 2011-11-02
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多