将错误消息从php发送到ajax [英] Send error messages from php to ajax
问题描述
我正在尝试从php向ajax发送通知"或错误消息.我正在尝试实现以下目标:
I am trying to send a "notification" or error messages from php to ajax. I'm trying to achieve something like this:
php:
if (myString == '') {
// Send "stringIsEmpty" error to ajax
} else if (myString == 'foo') {
// Send "stringEqualsFoo" error to ajax
}
ajax
$.ajax({
url: $(this).attr("action"),
context: document.body,
data: formData,
type: "POST",
contentType: false,
processData: false,
success: function(){
alert("It works");
},
error: function() {
if(stringIsEmpty) {
alert("String is empty");
} else if(stringEqualsFoo) {
alert("String equals Foo");
}
}
});
如何向ajax发送错误消息?
How can I send error messages to ajax?
更新
这是我拥有的php文件.我尝试使用echo
解决方案回答说,但是当我输出data
是什么(在ajax中)时,我得到了undefined
:
Here's the php file I have. I tried using the echo
solution answers said, but when I output what the data
is (in ajax), I get undefined
:
<?php
$img=$_FILES['img'];
if($img['name']==''){
echo('noImage');
}else{
$filename = $img['tmp_name'];
$client_id="myId";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$pvars = array('image' => base64_encode($data));
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$out = curl_exec($curl);
curl_close ($curl);
$pms = json_decode($out,true);
$url=$pms['data']['link'];
if($url!=""){
echo "<h2>Uploaded Without Any Problem</h2>";
echo "<img src='$url'/>";
}else{
echo "<h2>There's a Problem</h2>";
echo $pms['data']['error'];
header("HTTP/1.1 404 Not Found");
}
}
?>
我在if($img['name']==''){
推荐答案
仅当请求失败时,才会调用错误函数,请参见 http://api.jquery.com/jQuery.ajax/
The error function will only be called if the request fails, see http://api.jquery.com/jQuery.ajax/
因此,如果您从PHP服务器返回响应,则不会触发错误功能.但是,您可以根据从PHP发送的响应定义一个函数来处理错误:
So if you return a response from your PHP server, the error function won't be triggered. However, you can define a function to handle the error based on the response you send from PHP:
success: function(data){
if (data === "stringIsEmpty") {
triggerError("stringIsEmpty");
} else if (data === "stringEqualsFoo") {
triggerError("stringEqualsFoo");
}
},
然后您可以具有以下错误功能:
And then you can have the error function like this:
function triggerError(error) {
if (error === "stringIsEmpty") {
alert("Your string is empty!");
} else if (error === "stringEqualsFoo") {
alert("Your string is equal to Foo!");
}
}
如果您请求发送post.php,则只需返回一个字符串即可:
If you make a request to let's say post.php, you can just return a string:
// Create a function to see if the string is empty
$funcOutput = isStringEmpty();
echo $funcOutput;
或者专门用于示例:
echo "stringIsEmpty";
有关更多信息,请参见:如何从PHP返回数据到jQuery ajax调用
For more information see: How to return data from PHP to a jQuery ajax call
这篇关于将错误消息从php发送到ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!