jquery ajax调用 - 如果dataType只是一个文本(字符串),则获取返回值

yor*_*gos 8 jquery

我有以下代码.

    $.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";
    }
}); 
Run Code Online (Sandbox Code Playgroud)

1)无需发送数据.

2)文件"blahblah.php"进行一些处理并返回true或false.

3)我想得到响应,如果真的重定向到另一个php页面.

我不知道如何在blahblah.php文件中读取函数的返回数据!!

先感谢您.乔治

Set*_*eth 15

您必须将变量传递给success调用.

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

        console.log(data);

    }
}); 
Run Code Online (Sandbox Code Playgroud)

如果成功方法运行,那么您已成功提交文件并可以使用简单方法重定向 document.location = "http://www.example.com/somewhere.php


Kon*_*Kon 2

您是否尝试过将 dataType 设置为'json'?此外,您实际上并没有在成功回调时从服务器检索响应。尝试这个:

    $.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";
         }
      }
   }); 
Run Code Online (Sandbox Code Playgroud)