2017-02-23 43 views
2

我寫一個網頁,要在使用PHP MySQL查詢使用HTML表格中讀取數據。這是this question的延續。我得到AJAX發送我需要使用的數據到PHP文件的代碼來更新它發送的信息。但是,發生了兩個錯誤。AJAX做什麼,我需要做的,但錯誤仍時有發生

  1. 我得到一個消息說Error:Error: jQuery21405680291895882033_1487801210725 was not called

  2. 我送有一個數據:在它的結束,給我一個錯誤「1」。

如何解決這兩個錯誤?非常感謝!

JS代碼:

function getTableData(){ 
      var array = []; 
      var headers = []; 
      $('#tablaListado th').each(function(index, item) { 
       headers[index] = $(item).html(); 
      }); 
      $('#tablaListado tr').has('td').each(function() { 
       var arrayItem = {}; 
       $('td', $(this)).each(function(index, item) { 
        if($(this).find("textarea").length){ 
         var costo = $(this).find('textarea').val(); 
         arrayItem[headers[index]] = costo; 
        }else{ 
         arrayItem[headers[index]] = $(item).html(); 
        } 
       }); 
       array.push(arrayItem); 
      }); 

      actualizarCostos(array); 
     } 

     function actualizarCostos(array){ 
      if(array.constructor === Array){ 
       for(var i = 0; i < array.length ; i++){ 
        console.log(array[i]); 
        console.log(JSON.stringify(array[i])); 
        $.ajax({ 
         url: "http://www.page.com/Update.php", 
         contentType: "application/json; charset=utf-8", 
         dataType: "jsonp", 
         jsonp: false, 
         jsonpCallback: jsonCallback, 
         cache: true, 
         data:{ "table_data": JSON.stringify(array[i])}, 
         success: function (data) { 
          console.log(data); 
         }, 
         error: function(xhr,status,err){ 
          alert("DEBUG: status"+status+" \nError:"+err); 
         }, 
         traditional: true 
        }); 
       } 
      }else{ 
       alert("Input must be array"); 
      } 
     } 

     function jsonCallback(data, status){ 
      console.log(data + " " + status); 
     } 

PHP部分:

//Added on top of the page 
header('Access-Control-Allow-Origin: *'); 
... 
function updateCosts($json){ 
      conectar(); 
      $array = json_decode($json, true); 
      echo "<script>console.log('$array');</script>"; 
      $costo = $array["Costo"]; 
      $sku = $array["SKU"]; 
      $instruccion = "UPDATE articulos SET art_cost='$costo' WHERE art_SKU = '$sku'"; 

      return ejecutarInstruccion($instruccion); 
} 

if(isset($_GET['table_data'])){ 
      foreach($_GET['table_data'] as $index => $item) 
      { 
        // do something here 
        echo "<script>console.log('$item');</script>"; 
        updateCosts($item); 
      } 

      // Response back - if needed 
      echo json_encode(array('status'=>true, 'msg'=>'Some Message')); 
} 

編輯:

我忘了提,我試圖改變這一代碼 'JSONP' 爲 'JSON',但我收到一個錯誤,說PHP文件不允許外部數據,即使我在所述文件上使用header('Access-Control-Allow-Origin: *')

回答

2

你的頁面返回JSON,而不是JSONP。它們不可互換。從$.ajax()通話取出jsonpjsonpCallback屬性和更改dataTypejson

$.ajax({ 
    url: "http://www.page.com/Update.php", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    cache: true, 
    data: { 
     table_data: JSON.stringify(array[i]) 
    }, 
    success: function (data) { 
     console.log(data); 
    }, 
    error: function(xhr,status,err){ 
     alert("DEBUG: status" + status + " \nError:" + err); 
    }, 
    traditional: true 
}); 
+0

謝謝你提醒我,我編輯的職位與信息,爲什麼我不能用'。'json'' –

+0

我需要的'接入控制允許Origin'頭從PHP頁面添加到響應這種情況下, ,所以瀏覽器明確知道它不應該由於同源策略而阻止這個請求。正如我上面提到的,JSONP與JSON完全不同。要使用這將意味着完全改變PHP代碼的響應格式。 –

+0

我將它添加到PHP文件的頂部,它是否在if語句上呢? –

1

我建議你有以下AJAX:

function actualizarCostos(array){ 
     if(array.constructor === Array){ 
      for(var i = 0; i < array.length ; i++){ 
       console.log(array[i]); 
       console.log(JSON.stringify(array[i])); 
      } 
      $.ajax({ 
       url: "http://www.page.com/Update.php", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       data:{ "table_data": JSON.stringify(array)}, 
       success: function (data) { 
        console.log(data); 
       }, 
       error: function(xhr,status,err){ 
        alert("DEBUG: status"+status+" \nError:"+err); 
       } 
      }); 
     }else{ 
      alert("Input must be array"); 
     } 
    } 

然後執行for循環的PHP服務器端。也許在array每個變量都具有隱藏的參數就知道自己的索引,或者JSON是做一些奇怪的像數組中顯示的元素數量(在這種情況下,1,因爲你遍歷各一個)

+0

如果我遍歷每一個不會給我一個':1'和':2'嗎? –

+0

'或JSON做了一些奇怪的事情,比如顯示數組元素的數量,這會在每個項目上給出':1',或者在23個項目列表上給出':23' ...我不是100%的,沒有調試就很難說...... – SsJVasto