我所要做的就是从用户输入的输入元素构建一个对象,而不用单击提交。这些函数应该完成这项工作,除了我没有在主页上正确获取$ _POST数组:
我开始认为我可以创建一个简单的json对象并将其字符串化,然后将其作为'json'数据类型ajax。那没用。我不断得到解析器错误,'意外的角色......'。我使用外部解析器来检查生成的对象,并且每次都有效。 所以我修改了我的序列化来创建一个jquery.param对象,如下所示。
console.log中的输出如下所示:
[ [{ hostname: 'myhostname'}, {userdb1:'mydbname'}, {adminname:'guest'}, {adminpassword:'abc123'}, {username:'muuser'}, {password:'myuser'} ]
主机上的var_dump [$ _ POST]是:
Array ( [0] => [ [1] => { [2] => [3] => h [4] => o [5] => s [6] => t [7] => n [8] => a [9] => m [10] => e [11] => : [12] => [13] => ' [14] => m [15] => y [16] => h [17] => o [18] => s [19] => t [20] => n [21] => a [22] => m [23] => e [24] => ' [25] => } [26] => , [27] => [28] => { [29] => u [30] => s [31] => e [32] => r [33] => d [34] => b [35] => 1 [36] => : [37] => ' [38] => m [39] => y [40] => d [41] => b [42] => n [43] => a [44] => m [45] => e [46] => ' [47] => } [48] => , [49] => [50] => { [51] => a [52] => d [53] => m [54] => i [55] => n [56] => n [57] => a [58] => m [59] => e [60] => : [61] => ' [62] => g [63] => u [64] => e [65] => s [66] => t [67] => ' [68] => } [69] => , [70] => [71] => { [72] => a [73] => d [74] => m [75] => i [76] => n [77] => p [78] => a [79] => s [80] => s [81] => w [82] => o [83] => r [84] => d [85] => : [86] => ' [87] => a [88] => b [89] => c [90] => 1 [91] => 2 [92] => 3 [93] => ' [94] => } [95] => , [96] => [97] => { [98] => u [99] => s [100] => e [101] => r [102] => n [103] => a [104] => m [105] => e [106] => : [107] => ' [108] => m [109] => u [110] => u [111] => s [112] => e [113] => r [114] => ' [115] => } [116] => , [117] => [118] => { [119] => p [120] => a [121] => s [122] => s [123] => w [124] => o [125] => r [126] => d [127] => : [128] => ' [129] => m [130] => y [131] => u [132] => s [133] => e [134] => r [135] => ' [136] => } [137] => [138] => ] )
这里发生了什么。需要获取这些键/值简单用户创建对象的$ _POST数组,而不需要表单序列化!
以下是功能
function savePageInputs(idtags,targetOutId)
{
var method = 'post';
var phpscript = 'savedata.php';
var data = new Array();
for(var i=0;i<idtags.length;i++)
{
tag = idtags[i];
data[i] = $(\"#\"+tag).val(); //get their values
}
jsonData = makeJsonData(idtags,data); //make JSON object
shipJsonToHost(jsonData, phpscript, method, targetOutId);
}
function makeJsonData(idtags,values)
{
tag = idtags[0];
val = values[0];
jsonData = '[{ '+ tag +': '+'\''+val+'\'}' ;
for(var i=1;i<idtags.length;i++)
{
tag = idtags[i];
val = valueList[i];
jsonData += ', {' + tag+':'+ '\''+val+'\'}' ;
}
jsonData += ' ]' ;
return jsonData;
}
function shipJsonToHost(jsonData, phpscript, method, targetOutId)
{
jQuery.ajaxSettings.traditional = true;
var outputTo = $(\"#\"+targetOutId);
outputTo.append('<center>Processing...<br/> <img src=\"images/loading.gif\" height=50 width=50></center>');
$.ajax
(
{
data: $.param(jsonData) ,
type: method,
dataType: 'text',
url: phpscript,
success: function(response)
{
outputTo.empty();
outputTo.append(response);
},
error: function(request, errorType, errorThrown)
{
outputTo.html('Error: '+errorType+ ' '+errorThrown);
}
}
);
}
答案 0 :(得分:3)
不要忘记json的实际语法
[{test: 1}]
无效[{'test': 1}]
无效[{"test": 1}]
有效[{"value": 'test'}]
无效[{"value": "test"}]
有效尝试用一些双引号替换makeJsonData函数中的所有单引号;通常可以解决这个问题