我想将后面的json字符串作为一个参数和其他参数发送给后端。我希望以jsonp的形式收到结果。
参数是
$param1 = ' {
"MetaData": [
{"Index": "0", "Name": "COLUMN_NAME"},
{"Index": "1", "Name": "VALUE"},
{"Index": "2", "Name": "VALUE_CHANGED"}
],
"Data": [
["ORDER_NO","*2733","f"],
["DISCOUNT_NO","1","f"],
["DISCOUNT_TYPE","S1","f"],
["DISCOUNT","11.4","t"],
]
} ';
我应该如何在js中编写语法?我应该如何从php端接收它?当我尝试像下面那样使用它时它不起作用。
$.ajax({
url: 'http://... myfile.php',
data: {PARAMETER1: $param1, PARAMETER2: $param2}
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
error: function() { alert('Failed!'); }
}).done(function( data ) {
$.each(data, function(k,v) {
alert( "key: "+k+" val:" + v);
})
});
答案 0 :(得分:1)
您的Javascript代码对我来说没问题。在PHP方面,它将是:
$param1 = json_decode($_GET['PARAMETER1'], true);
然后,您可以访问$param1['MetaData'][$i]['Index']
,例如。
答案 1 :(得分:1)
您正在发送包含两个字符串的对象
在您的PHP代码中,您应该使用json_decode
将它们解码为json这取决于您的后端代码/框架...只需读取它们然后解码它们
答案 2 :(得分:0)
$(document).ready(function(){
param1 = ' { "MetaData": [ {"Index": "0", "Name": "COLUMN_NAME"},
{"Index": "1", "Name":"VALUE"}, {"Index": "2", "Name": "VALUE_CHANGED"} ],
"Data": [ ["ORDER_NO","*2733","f"], ["DISCOUNT_NO","1","f"],
["DISCOUNT_TYPE","S1","f"],
["DISCOUNT","11.4","t"], ] } ';
param2 ='2';
$.ajax({ url: 'ajaxresponse.jsp',
type: 'POST',
data:'{"PARAMETER1":'+ param1+',"PARAMETER2":'+ param2+'}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function(msg) { alert(msg); } });
});