Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax call - get returning value if dataType is simply a text (string)

Tags:

jquery

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

like image 381
yorgos Avatar asked Jun 11 '11 15:06

yorgos


People also ask

How can I get specific data from AJAX response?

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.

Which is used to get the response of an AJAX call as a string?

AJAX - Server Response The XMLHttpRequest object has an in-built XML parser. The responseXML property returns the server response as an XML DOM object.

What is the return type of AJAX call?

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.

How can a function return a value in AJAX?

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


3 Answers

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";
         }
      }
   }); 
like image 36
Kon Avatar answered Nov 15 '22 21:11

Kon


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

like image 37
Sangeet Shah Avatar answered Nov 15 '22 20:11

Sangeet Shah


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

like image 171
Seth Avatar answered Nov 15 '22 22:11

Seth