1

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! \n 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';
GeoffWilson
  • 413
  • 7
  • 20

2 Answers2

4

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...

Community
  • 1
  • 1
Mr.Manhattan
  • 4,795
  • 3
  • 18
  • 30
  • I am still revieving an error when i take out the `dataType` property. – GeoffWilson Jun 26 '14 at 07:55
  • what's the message? did you check in a network console what is returned? (for example in firebug/firefox web console) – Mr.Manhattan Jun 26 '14 at 07:57
  • when i look at the network tab it just says cancelled under the Status column. – GeoffWilson Jun 26 '14 at 07:59
  • are you working on the same domain? if it's a cross domain call, try `crossDomain: true` – Mr.Manhattan Jun 26 '14 at 08:05
  • That still calls the `error:`. It is cross domain but i cannot see what the problem is – GeoffWilson Jun 26 '14 at 08:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56322/discussion-between-g-wilson-and-mr-manhattan). – GeoffWilson Jun 26 '14 at 08:11
  • cross domain can cause all sorts of errors... like http://forum.jquery.com/topic/ajax-call-chrome-status-text-cancelled or http://stackoverflow.com/questions/7577275/jquery-ajax-requests-are-getting-cancelled-without-being-sent or http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – Mr.Manhattan Jun 26 '14 at 08:11
-1

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

echo json_encode($JSON_Data);
OGHaza
  • 4,683
  • 7
  • 21
  • 29
amit 1984
  • 374
  • 2
  • 9
  • That's not very helpfull, First: $JSON_Data is the input delivered to the file, Second: by just adding this line, the file would still contain `echo`'s, which would break the json, Third: i already postet a solution containing that information. – Mr.Manhattan Jun 26 '14 at 08:17