I am trying to do a POST request in ajax in this way:
$.ajax({
type: "POST",
url: 'http://192.168.1.140/',
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
},
error: function (data, success, failure) {
alert("Error:" + failure);
}
});
The server I have at that IP only do this:
<?php
echo "test"
?>
In the alert I only get the word 'Error' from the error function and nothing else from the parameters. I don't know where is the problem. I want it to display in the alert the word 'test' that the servers sends. Can anyone tell me what I am doing wrong?
The reason you're getting an error is because you're telling jQuery ajax that this request will return a json response but you just return plain text.
I'm not sure why your errorThrown is blank as it should show parse error.
You can just have alert("Error:" + data.responseText); to alert the result.
To avoid the error you can echo json in your php script echo '{"test":true}'
or remove the dataType: "json", in your request.