我寫一個網頁,要在使用PHP MySQL查詢使用HTML表格中讀取數據。這是this question的延續。我得到AJAX發送我需要使用的數據到PHP文件的代碼來更新它發送的信息。但是,發生了兩個錯誤。AJAX做什麼,我需要做的,但錯誤仍時有發生
我得到一個消息說
Error:Error: jQuery21405680291895882033_1487801210725 was not called
我送有一個數據:在它的結束,給我一個錯誤「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: *')
。
謝謝你提醒我,我編輯的職位與信息,爲什麼我不能用'。'json'' –
我需要的'接入控制允許Origin'頭從PHP頁面添加到響應這種情況下, ,所以瀏覽器明確知道它不應該由於同源策略而阻止這個請求。正如我上面提到的,JSONP與JSON完全不同。要使用這將意味着完全改變PHP代碼的響應格式。 –
我將它添加到PHP文件的頂部,它是否在if語句上呢? –