[英]jQuery ajax post request not sending data
我正在使用 jQuery ajax 向我的 php 代碼發送 post 請求,但它沒有發送數據。
var uploadData = {
'email': email
};
$.ajax({
url: './upload.php',
method: 'POST',
dataType: 'json',
data: uploadData,
contentType: "application/json; charset=utf-8",
success: function(data, status, xhr) {
console.log(xhr.responseText);
return(true);
},
error: function(data, status, xhr) {
console.log(xhr.responseText);
return(false);
}
});
我嘗試添加dataType:'json',
和contentType: "application/json; charset=utf-8",
嘗試更改 post 以獲取,但沒有任何改變。
我認為它就像這樣:
$.ajax({
url: './upload.php',
type: 'POST',
dataType: 'json',
data: {
'email': 'test@gmail.com'
},
success: function(data) {
return data;
}
});
該代碼對我有用。
您可以使用以下方法獲取純請求負載:
$raw = file_get_contents('php://input');
然后您可以將其從 json 解碼為數組:
$data = json_decode($raw, true);
並打印數組:
print_r($data);
首先,不需要 JSON.stringify。
在 $.ajax 調用中,如果 jquery 版本 >= 1.9.0 並且“type”用於舊版本,則使用 configurain 對象的成員“method”。 ( http://api.jquery.com/jquery.ajax/ )這是一個片段:
$.ajax({
url: './upload.php',
method: "POST", //**'type' if jquery v < 1.9.0 **
dataType: 'json',
data: {
email: "test@gmail.com"
},
.....................
................
});
對於未來的 googlers,如果您一直嘗試通過post
發送數組,並且已將數組初始化為
var blah = [];
嘗試將其設為如下對象,看看它是否有效。
var blah = {};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.