0

我的ajax调用有问题。当我尝试从我的 PHP 发出请求时,ajax 在错误函数中返回,我不知道为什么,因为一切正常。我的 PHP 中的所有数据都正确插入,我在我的 PHP 中返回了类似的内容:echo json_encode($response);当我检查浏览器网络然后导航我的 ajax 请求时,它给了我成功,但在我的 ajax 回调成功函数中,它没有而是执行它会出错。

这是我的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);
    }
    });

在此处输入图像描述

4

1 回答 1

1

假设您返回的数据确实是 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 在您的情况下不为空。

于 2020-12-29T04:26:45.567 回答