weixin_33698043 2014-06-26 07:48 采纳率: 0%
浏览 68

$ .ajax调用中的错误

I have an $.ajax call in one of my pages that links to a simple php page.

I am getting my alert for the error: property. I am not getting anything back in the errorThrown variable or in the jqXHR variable. I have never done this kind of thing before and i am not seeing what is wrong with my page.

JQuery $.ajax call :

    function jsonSync(json) {
        $.ajax({
            type: 'POST',
            url: 'http://www.cubiclesandwashrooms.com/areaUpdate.php',
            dataType: 'json',
            data: json,
            context: this,
            success: function () {

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('Error has occured! 
 ERR.INDEX: Sync failed, ' + jqXHR.responseText + ';' + textStatus + ';' + errorThrown.message);
                return false;
            }
        });

And this is my PHP Page :

$JSON = file_get_contents('php://input');
$JSON_Data = json_decode($JSON);

//handle on specific item in JSON Object
$insc_area = $JSON_Data->{'insc_area'};

//mysqlite connection.open() equivilent
$insc_db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno($insc_db)) {
    die('Could not connect: ' . mysql_error());
    echo "Failed to connect to MySql: " . mysqli_connect_error();
    //mysqli_close($insc_db);
}

//$insc_area.length equivilent
$insc_area_size = sizeof($insc_area);

//cycle through reult set
for ($i = 0; $i < $insc_area_size; $i++) {

    //assign row to DataRow Equivilent
    $rec = $insc_area[$i];

    //get specific column values
    $area = $rec->{'area'};
    $id = $rec->{'srecid'};

    //sqlcommand equivilent
    $query = "SELECT * FROM insc_products WHERE id='$id' LIMIT 1";
    $result = mysqli_query($insc_db, $query);
    $num = mysqli_num_rows($result);

    //dataReader.Read equivilent
    while ($row = $result->fetch_array()) {

        $query = "UPDATE insc_products SET area='$area' where id = '$id'";
        $res = mysqli_query($insc_db, $query);

        //checking if update was successful
        if ($res) {
            // good
            error_log('user update done');
            echo 'update was successful';
        } else {
            error_log('user update failed');
            echo 'error in update';
        }
    }
}
echo 'testing php';
  • 写回答

2条回答 默认 最新

  • 喵-见缝插针 2014-06-26 07:51
    关注

    dataType: 'json'

    means: give me json back. your PHP file isn't returning json formatted data

    similar question: jQuery ajax call returns empty error if the content is empty

    to buid a json response fill an array in the php file with the return information and use echo json_encode($array); at the end of the file. if you are using dataType:'json' because the code is copy/pasted, and you won't need the response to be in json format, simply remove this option...

    评论
  • weixin_33743880 2014-06-26 08:14
    关注

    Add following line in php file, $JSON_Data encode then it will work.

    echo json_encode($JSON_Data);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 SQL中的图片,无法传回php到前端。没有任何报错。
  • ¥15 oled显示有问题,初始化后应该啥也不显示,但却亮了一大片
  • ¥15 【通信原理】为什么传信率不变?频带利用率为啥没有二倍
  • ¥15 CANOPEN SDO
  • ¥15 r语言数据集循环获取问题
  • ¥30 求佬们帮助,总是出bug,求佬们解决一下bug
  • ¥15 后端Java转换字符串传给前端,前端如何解析呢?
  • ¥15 010editor导入文件后一直是只读
  • ¥15 psychopy(python为基础的)中引入cmd
  • ¥15 不知道怎么去做关于前端电子请柬