将 php 响应发送到 ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10114852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:24:39 来源:igfitidea点击:
Send php response to ajax
提问by lostAstronaut
Okay I have a php script which ends as so :
好的,我有一个以这样结尾的 php 脚本:
if ($success)
{
$result = array('success' => true);
}
else
{
$result = array('success' => false, 'message' => 'Something happened');
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
}
echo json_encode($result);
And some jquery that I was planning on having alert me when my script is working.
还有一些我计划在我的脚本工作时提醒我的 jquery。
jQuery(document).ready(function() {
$.ajax({
url: './contactengine.php',
type: 'GET',
dataType: 'JSON',
success: function(response) {
alert("GOOD");
},
error: function() {
alert("BAD");
}
});
});
edited source
编辑源
回答by Ryan
<?php
if ($success){
$result = array("status" => "1");
echo json_encode($result);
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=/404.html\">";
}
?>
<script>
jQuery(document).ready(function() {
$.ajax({
type: 'GET',
url: 'Thatscriptsomething.php',
cache: 'false',
dataType: 'json',
success: function(response) {
if(response.status == "1") {
alert("we having a working script");
} else {
alert("Oops, script is a no go");
}
}
});
});
</script>
回答by Asad Ali
Basic example - it works for me
基本示例 - 它对我有用
PHP RESPONSE
PHP 响应
$value = array('msg' => 'true' );
echo json_encode($value);
AJAX METHOD
AJAX 方法
$.ajax({
type: 'post',
url: 'URL',
contentType: false,
processData: false,
dataType:'JSON',
data: formData,
success: function(value) {
if (value.msg == 'true') {
//your action
}else{
//your action
}
}
});

