[英]JQuery ajax call gets 400 bad request
我正在使用以下JS代碼發送ajax請求:
var data = JSON.stringify({
'user_id': '<?=$user->id?>',
'package_id': '<?=$bundle->package_id?>',
'YII_CSRF_TOKEN': '<?=Yii::app()->request->csrfToken?>'
});
$.ajax({
url: "/bundle/ajaxRemove",
data: data,
type: "POST",
contentType: "application/json",
dataType: "json",
error: function (xhRequest, ErrorText, thrownError) {
alert("Failed to process user correctly, please try again");
console.log('xhRequest: ' + JSON.stringify(xhRequest) + "\n");
console.log('ErrorText: ' + ErrorText + "\n");
console.log('thrownError: ' + thrownError + "\n");
}
}).done(function (msg) {
console.log(msg);
});
問題是,每當我提交此代碼時,服務器都會不斷返回400 Bad Request Error消息。
我已經嘗試過提交作為post數據而不是json數據,並且仍然不斷收到相同的錯誤。 在這里撕扯我的頭發!
知道我可能做錯了什么嗎?
$.ajax({
dataType: 'json',
type: 'POST',
contentType: false,
data: {
user_id: '<?=$user->id?>',
package_id: '<?=$bundle->package_id?>',
YII_CSRF_TOKEN:'<?=Yii::app()->request->csrfToken?>'},
url: '<?php echo Url::to(["/bundle/ajaxRemove"]); ?>',
success: function(data) {
},
error: function (xhRequest, ErrorText, thrownError) {
alert("Failed to process user correctly, please try again");
console.log('xhRequest: ' + JSON.stringify(xhRequest) + "\n");
console.log('ErrorText: ' + ErrorText + "\n");
console.log('thrownError: ' + thrownError + "\n");
}
});
試試這個。
根據定義,JSON數據必須用雙引號而不是單引號。
如果使用ajax發送數據,則給ajax JSON數據然后自己進行轉換要容易得多。
接下來的一點是:您是要在JS中使用PHP還是要發送例如:
"<?=$user->id?>"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.