I have the following code.
$.ajax({
type: "POST",
url:"blahblah.php",
data: "{}",
async: true,
dataType: "text",
success: function() {
if(returning data from blahblah.php == true)
window.location.href="http://www.blahblah.com/logout.php";
}
});
1) No data need to be sent.
2) File "blahblah.php" does some processing and returns either true or false.
3) I want to get the response and If true redirect to another php page.
I do not know how to read the returning data of the function in the blahblah.php file!!
Thank you in advance. George
You can't as it's asynchronous. If you want to do anything with it, you need to do it in a callback. How? Because it's asynchronous, javascript will fire off the ajax request, then immediately move on to execute the next bit of code, and will probably do so before the ajax response has been received.
AJAX - Server Response The XMLHttpRequest object has an in-built XML parser. The responseXML property returns the server response as an XML DOM object.
The server should return valid JavaScript that passes the JSON response into the callback function. $.ajax() will execute the returned JavaScript, calling the JSONP callback function, before passing the JSON object contained in the response to the $.ajax() success handler.
You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });
Have you tried setting dataType to 'json'
?
Also, you're not actually retrieving the response from the server on a successful callback. Try this:
$.ajax({
type: "POST",
url:"blahblah.php",
data: "{}",
async: true,
dataType: "json",
success: function(response) {
if(response == true) {
window.location.href="http://www.blahblah.com/logout.php";
}
}
});
If your success event is not call then you may be get return data using complete event
example
complete: function (data) {
alert(data.responseText);
},
using this you can get response data in this event
You have to pass a variable into the success
call.
$.ajax({
type: "POST",
url:"blahblah.php",
data: "{}",
async: true,
dataType: "text",
success: function( data ) {
console.log(data);
}
});
If the success method runs then you have successfully submitted the file and can redirect with a simple document.location = "http://www.example.com/somewhere.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With