簡體   English   中英

AJAX做我需要做的事,但是仍然會發生錯誤

[英]AJAX does what I need it to do, but errors still occur

我正在寫一個網頁,該網頁從html表中讀取數據,以便在使用PHP的MySQL查詢中使用。 這是這個問題的延續。 我讓AJAX將需要使用的數據與代碼一起發送到PHP文件,以更新其發送的信息。 但是,發生了兩個錯誤。

  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: *')在上述檔案上。

您的頁面返回的是JSON,而不是JSONP。 它們是不可互換的。 $.ajax()調用中刪除jsonpjsonpCallback屬性,並將dataType更改為json

$.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
});

我建議您使用以下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");
        }
    }

然后在服務器的PHP端執行For-Loop。 也許array中的每個變量都有一個隱藏的參數來知道其自己的索引,或者JSON做一些奇怪的事情,例如顯示數組中的元素數(在這種情況下為1,因為您要遍歷每個變量)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM