[英]Ajax returning into error and not in success
我的 ajax 通話有問題。 當我嘗試從我的 PHP 發出請求時,ajax 返回錯誤 function,我不知道為什么,因為一切正常。 我的 PHP 中的所有數據都已正確插入,我在 PHP 中返回了類似的內容: echo json_encode($response);
當我檢查瀏覽器網絡然后導航我的 ajax 請求時,它給了我成功,但是在我的 ajax 回調成功 function 中,它不會執行錯誤。
這是我的 ajax 請求:
let fileInput = $('#file_input')[0];
let postData = new FormData();
$.each(fileInput.files, function(k,file){
postData.append('requirement_files[]', file);
});
let other_data = $('#new_foster_applicant_frm_id').serializeArray();
$.each(other_data,function(key,input){
postData.append(input.name,input.value);
});
$.ajax({
url : baseurl+'user/post/new_foster_applicant',
type : 'POST',
data : postData,
contentType: false,
processData: false,
success : function(response) {
if(response.success === 'true') {
console.log('true');
// $('.returen_savs').html('Please copy the transaction code serve as your transaction receipt...');
// $('.error_hadble').html('<div class="alert alert-success fade in m-b-15">'+'Transaction Code:'+response.transaction_code+'</div>');
}else {
console.log('false');
// $('.returen_savs').html('');
// $('.error_hadble').html('<div class="alert alert-danger fade in m-b-15">'+'Error in submitting. Please try again later'+'</div>');
}
},
error : function(e) {
console.log(e);
}
});
假設你返回的數據確實是 JSON 類型。
請
(1) 刪除 contentType:false 和 processData:false 語句
(2)使用方法而不是類型'POST'(假設您使用的是新版本)
(3) 添加JSON.parse 解析返回的JSON數據
因此,改變
$.ajax({
url : baseurl+'user/post/new_foster_applicant',
type : 'POST',
data : postData,
contentType: false,
processData: false,
success : function(response) {
if(response.success === 'true') {
至
$.ajax({
url : baseurl+'user/post/new_foster_applicant',
method : 'POST',
data : postData,
// contentType: false,
// processData: false,
success : function(response) {
var obj = JSON.parse(response);
if(obj.success === 'true') {
在您的情況下,還請確保 postData 不是 null。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.