我正在尝试使用 AJAX
和 JSON
将表单数据发布到另一个页面。这是我的 AJAX 代码。
var myData = '{ "number1": ' + $("#text1").val() + ', "number2": ' + $("#text2").val() + ' }';
$.ajax({
url: $("form").attr("action"),
type: 'POST',
success: function(response){
var p = $("p");
p.append(response);
//console.log(response);
},
error: function(request,error,msg){
$("p").html(error+": "+msg);
},
data: myData,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
});
我正在目标页面上使用 print_r
函数打印 $_POST
数组,以查看是否收到任何参数。但我发现它是空的。在响应中获取 parsererror: SyntaxError: JSON.parse: JSON 数据第 1 行第 1 列出现意外字符
。
最佳答案
用这个
var centercode = $('#WONum').val(); var centerName = $('#WONum2').val();
//Now you have the value of the textbox, you can do something with it, maybe an AJAX call to your server!
var centerObj = {};
centerObj["centercode"]= $('#WONum').val();
centerObj["centerName"]= $('#WONum2').val();
var jsonData = ko.toJSON(centerObj);
console.log(jsonData);
$.ajax({
url: web_service_url + "centermaster/createCenterMaster", ///"+branchcode+"/"+branchname,
data: jsonData,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: "jsonp",
关于php - AJAX 未发布 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837892/