2017-02-23 44 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