var ajaxData = {1: a, 2: b, 3: c, 4: d};
$.ajax({
type: "POST",
data : ajaxData,
etc
etc
如果我在$ ajax中使用dataType: 'json'
,如何在我的php中捕獲數據?如果不是我可以使用$ _POST ['1']等,但我打算使用賈森,因爲我必須做$_POST['2']
和更多..
var ajaxData = {1: a, 2: b, 3: c, 4: d};
$.ajax({
type: "POST",
data : ajaxData,
etc
etc
如果我在$ ajax中使用dataType: 'json'
,如何在我的php中捕獲數據?如果不是我可以使用$ _POST ['1']等,但我打算使用賈森,因爲我必須做$_POST['2']
和更多..
數據類型:JSON是Ajax調用的返回數據類型
數據是你的PHP文件/ servlet將被期待的參數。傳遞一個JSON見下面的例子,它是一個混合的JSON和正常參數
var params = {a:1,
b:2,
myJSON: JSON.stringify({data1:1, data2:2, data3:3})}
jQuery.ajax({
type: "POST",
url: "some/url/of/php/file",
data: params,
dataType: "json",
success: function(response){
// this should catch what ever is returned by your php file
alert(response)
},
error: function(msg) {
alert('error');
}
});
在你的PHP文件中,您可以訪問所傳遞的數據是這樣的$ _ POST [「一」],$ _ POST ['B '],$ _POST ['myJSON']
dataType是您的php文件的返回類型,而不是發送到php文件的參數。
參考這裏瞭解更多信息 http://api.jquery.com/jquery.ajax/
您將有一個名爲數據的請求中的php參數,將被解析爲Json ,那麼你只需要申請:
$data = json_decode($_POST[1]);
我會去與
$.ajax(
...
data: {data: JSON.stringify({1: a, 2: b, 3: c, 4: d};)}
)
然後在服務器端我會使用json_decode函數來獲取數據
好了,現在在PHP端的問題,我有1進行檢查1?我的意思是像isset($ _ POST ['1']) – user3522738 2014-09-19 09:07:51
isset()檢查是否傳遞參數或獲取請求。在我的例子中,如果你想檢查參數a,b和myJSON是否被傳遞,你必須說 'if(isset($ _ POST ['myJSON'])){' '/ *在這裏用myJSON * /}' – andrex 2014-09-19 09:10:39
所以只需檢查整個json?必須在客戶端進行驗證? – user3522738 2014-09-19 09:34:53