0

I have a jQuery ajax call to update my database using a php script.

This is the call I make:

$.ajax({
        url: "update.php",
        type: 'POST',
        dataType: 'jsonp',
        data: {key1: value1, key2: value2},
        cache: false,
        error: function() {
            $("#failUpload").removeClass("hide");
        },
        success: function(data) {
            $("#succesUpload").removeClass("hide");
                    setTimeout(function() {
                        $("#succesUpload").addClass("hide");
                    }, 5000);
        }
   });

PHP Update part:

$key1 = $_POST["key1"];
$key2 = $_POST["key2"];

$con=mysqli_connect("localhost","username","password","dbname");

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "UPDATE TabelName SET ". $key2 ." ='". $key1 ."' WHERE id=1";

if ($result = mysqli_query($con, $sql)) {
    $resultArray = array();
    $tempArray = array();

    while ($row = $result->fetch_object()) {
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

}
mysqli_close($con);

The database updates and it works but in the console.log I receive this error message: POST http://domainname.com/file.php?callback=jQuery2110765103816287592_1432976576289 500 (Internal Server Error) When I open this I find this:

_.ajaxTransport.Y.cors.a.crossDomain.send @ jquery.js:26

I already searched and found about Cross Domain call stuff and you have to use jsonp etc but it didn't work out. Thx!

D_RBD
  • 341
  • 2
  • 7
  • Is there a specific reason you're using JSONP for a local request? Try using `dataType: 'json'`. Although, even this seems redundant as your PHP code does not appear to be returning any data in the response. – Rory McCrossan May 30 '15 at 11:08
  • have you close your ajax??? – Saty May 30 '15 at 11:09
  • I started with json but this gives me the same error... Then I found the cross domain ajax post which says to use jsonp but same error... – D_RBD May 30 '15 at 11:10
  • ajax is closed but not in question I wil edit it! – D_RBD May 30 '15 at 11:11
  • What is the 500 error exactly? That would seem to indicate a problem in your PHP code. – Rory McCrossan May 30 '15 at 11:12
  • 500 error is thrown by ajax call: This is what I see when I open the error: _.ajaxTransport.Y.cors.a.crossDomain.send _.extend.ajax – D_RBD May 30 '15 at 11:15
  • @RoryMcCrossan Thx for the tip! I watched the Apache error log and found that I fetch the object and shouldn't do that! It was a copy from previous code, thats the reason... Anyway thanx for the tip! – D_RBD May 30 '15 at 11:19

4 Answers4

1

Use following function for error. It will show the exact issue. I think it will help.

error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.responseText+errorThrown+textStatus);
                $("#failUpload").removeClass("hide");
        }

All the best.

Amit Jha
  • 331
  • 1
  • 11
1

With jsonp you can't send data using POST. jQuery $.ajax call has a wrong name because it's confused. When you do an $.ajax call with "JSON-P" data that functions injects a script on your DOM (< script src="example-domain.com/do-this-task.php?callback=my_callback_on_js>).

Do this:

  1. Use only $.ajax with JSON but ensure that you are on the same domain, if not, see point 2.
  2. If you are on localhost and you are calling other different domain, then you need to use jsonp (but only works for GET requests) OR enable CORS on server. See this post because I explain a similar problem like yours: local AJAX-call to remote site works in Safari but not in other browsers
Community
  • 1
  • 1
Jose Mato
  • 2,709
  • 1
  • 17
  • 18
1

For me the answer in this one was to delete:

dataType: 'json'

I found the answer here: jQuery returning "parsererror" for ajax request

Also I changed PHP fetch to:

if (mysqli_query($con, $sql)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($con);
}
Community
  • 1
  • 1
D_RBD
  • 341
  • 2
  • 7
1
$.ajax({
        url: "update.php",
        type: 'POST',
        dataType: 'jsonp',
        data: {key1: value1, key2: value2},
        cache: false,
        crossDomain: false,
        error: function() {
            $("#failUpload").removeClass("hide");
        },
        success: function(data) {
            $("#succesUpload").removeClass("hide");
                    setTimeout(function() {
                        $("#succesUpload").addClass("hide");
                    }, 5000);
        }
   });

put crossDomain: false, and try with this.

piyush
  • 554
  • 5
  • 14