Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何确保jQuery使用POST在标题中提交正确的JSON键?_Jquery_Json_Ajax_Post - Fatal编程技术网

如何确保jQuery使用POST在标题中提交正确的JSON键?

如何确保jQuery使用POST在标题中提交正确的JSON键?,jquery,json,ajax,post,Jquery,Json,Ajax,Post,我有这个片段 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 () { $(

我有这个片段

 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显示的内容,但当我看到标题时,它会显示以下内容:

columnName : mydatathatgotchanged
换句话说,它保留了列的名称,即“columnName”,然后是数据。它在某种程度上发生着变化。changedData值正确。

使用以下方法:

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

不能将变量用作Javascript对象名,请尝试使用[]符号输入对象,并使用变量名
data[columnName]=changedData
我想它现在可以用了。我试试看。
...
var data = {id: pKey};
data[columnName] = changedData;
...