我写一个网页,要在使用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语句上呢? –