jquery ajax调用 – 如果dataType只是一个文本(字符串),则获取返回值
我有以下代码。
$.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)无需发送数据。
2)文件“blahblah.php”进行一些处理并返回true或false。
3)我想得到响应,如果真的重定向到另一个php页面。
我不知道如何在blahblah.php文件中读取函数的返回数据!!
先感谢您。 乔治
您必须将变量传递给success
调用。
$.ajax({ type: "POST", url:"blahblah.php", data: "{}", async: true, dataType: "text", success: function( data ) { console.log(data); } });
如果成功方法运行,那么您已成功提交文件,并可以使用简单的document.location = "http://www.example.com/somewhere.php
重定向
您是否尝试将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"; } } });
如果您的成功事件未被调用,那么您可能会使用完整事件获得返回数据
例
complete: function (data) { alert(data.responseText); },
使用此function,您可以在此事件中获取响应数据