[英]My return functions isn't working with my jQuery ajax
I have this: 我有这个:
$("#upvote").click(function(){
var up = parseInt(document.getElementById('voteScore').innerHTML);
up++;
document.getElementById('voteScore').innerHTML = up;
$.ajax({
type: 'POST',
url: 'include/mysql_lib.php',
data: {'data[]':['upvote','<?php echo $id; ?>', '<?php echo $uid; ?>']},
dataType: "text",
success: function(dataType) {
if (dataType == "false") {
var up = parseInt(document.getElementById('voteScore').innerHTML);
up--;
document.getElementById('voteScore').innerHTML = up;
}
}
});
});
The mysql_lib.php
file (if an error is found) has a line like this: mysql_lib.php
文件(如果发现错误)具有如下一行:
return "false";
What am I doing wrong? 我究竟做错了什么? I've never used jQuery before.
我以前从未使用过jQuery。
The AJAX function's server response (the 'dataType' variable in your code) stores whatever your PHP script wrote-out to the server. AJAX函数的服务器响应(代码中的'dataType'变量)存储将PHP脚本写出到服务器的内容。 In your PHP script if you
return "false"
that will return a string from a function, but if you want to pickup the value in your JavaScript you should use echo "false"
so that the JavaScript's response from the server will be false
. 在您的PHP脚本中,如果
return "false"
,它将从函数返回一个字符串,但是,如果您想在JavaScript中获取值,则应使用echo "false"
以便服务器的JavaScript响应为false
。
function test() {
//do some work
return "false";
}
echo test();//this will output "false" to the browser
function test() {
echo "false";//this will output "false" to the browser
}
When running into issues like this it is a good idea to put a console.log(dataType)
or alert(dataType)
inside your AJAX callback function to see what is being output by your PHP. 当遇到这样的问题时,最好将
console.log(dataType)
或alert(dataType)
放入AJAX回调函数中,以查看PHP输出的内容。 The response from your PHP script can also be viewed in most developer tools (like FireBug). 您的PHP脚本的响应也可以在大多数开发人员工具(如FireBug)中查看。
And a suggestion for ya. 和一个建议。 If you want to get into outputting more complex information from your PHP script, take a look at the PHP json_encode() function which makes communicating between PHP and JavaScript painless.
如果要开始从PHP脚本输出更复杂的信息,请查看PHP json_encode()函数,该函数使PHP和JavaScript之间的通信变得轻松自如。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.