JQuery Ajax for PHP MySQL - Erro (500) Cross-Domain Internal Server

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

This is the call I'm making:

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

Part of the PHP update:

 $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 console.log I get 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 out about things with cross domains, and you need to use jsonp, etc., but that didn't work. thanks!

0
source share
4 answers

Use the following function for error. He will show the exact problem. I think this will help.

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

All the best.

+1
source

With jsonp, you cannot send data using POST. jQuery $ .ajax call has the wrong name because it is confusing. When you make a $ .ajax call with "JSON-P" data that injects a script into your DOM (<script src = "example-domain.com/do-this-task.php?callback= my_callback_on_js>).

Do it:

  • Use only $ .ajax with JSON, but make sure you are on the same domain, if not, see point 2.
  • If you are on a local host and you are invoking another different domain, then you need to use jsonp (but only works for GET requests) OR allows CORS on the server. See this post because I explain a similar problem such as yours: local AJAX call to a remote site works in Safari, but not in other browsers
+1
source

For me, the answer in this was to remove:

 dataType: 'json' 

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

I also changed PHP fetch to:

 if (mysqli_query($con, $sql)) { echo "Record updated successfully"; } else { echo "Error updating record: " . mysqli_error($con); } 
+1
source
 $.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.

+1
source

Source: https://habr.com/ru/post/1238750/


All Articles