Jquery ajax call - get return value if dataType is just text (string)

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 need to send data.

2) The file "blahblah.php" does some processing and returns either true or false.

3) I want to get an answer, and if true is redirected to another php page.

I don’t know how to read function return data in blahblah.php file!

Thanks in advance. George

+6
source share
3 answers

You must pass the variable to the success call.

 $.ajax({ type: "POST", url:"blahblah.php", data: "{}", async: true, dataType: "text", success: function( data ) { console.log(data); } }); 

If the success method is executed, you have successfully submitted the file and can redirect using a simple document.location = "http://www.example.com/somewhere.php

+12
source

Have you tried setting dataType to 'json' ? In addition, you do not actually receive a response from the server to a successful callback. Try the following:

  $.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"; } } }); 
+1
source

If your success event is not a challenge, you can get the data back using the full event

Example

 complete: function (data) { alert(data.responseText); }, 

using this, you can get response data in this case

+1
source

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