将错误消息从php发送到ajax

我试图从php发送“通知”或错误消息到ajax。 我正在努力实现这样的目标:

PHP:

if (myString == '') { // Send "stringIsEmpty" error to ajax } else if (myString == 'foo') { // Send "stringEqualsFoo" error to 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发送错误消息?

更新

这是我的php文件。 我尝试使用echo解决方案的答案说,但是当我输出data是什么(在ajax中)时,我得到了undefined

  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); https://stackoverflow.com/questions/33657714/send-error-messages-from-php-to-ajax/$url=$pms['data']['link']; if(https://stackoverflow.com/questions/33657714/send-error-messages-from-php-to-ajax/$url!=""){ echo "

Uploaded Without Any Problem

"; echo ""; }else{ echo "

There's a Problem

"; echo $pms['data']['error']; header("HTTP/1.1 404 Not Found"); } } ?>

我在if($img['name']==''){添加了echo("noImage") if($img['name']==''){

只有在请求失败时才会调用错误函数,请参阅http://api.jquery.com/jQuery.ajax/

因此,如果从PHP服务器返回响应,则不会触发错误function。 但是,您可以根据从PHP发送的响应定义一个函数来处理错误:

 success: function(data){ if (data === "stringIsEmpty") { triggerError("stringIsEmpty"); } else if (data === "stringEqualsFoo") { triggerError("stringEqualsFoo"); } }, 

然后你可以得到这样的错误函数:

 function triggerError(error) { if (error === "stringIsEmpty") { alert("Your string is empty!"); } else if (error === "stringEqualsFoo") { alert("Your string is equal to Foo!"); } } 

如果您发出请求让我们说post.php,您只需返回一个字符串:

 // Create a function to see if the string is empty $funcOutput = isStringEmpty(); echo $funcOutput; 

或者特别为例子:

 echo "stringIsEmpty"; 

有关更多信息,请参阅: 如何将数据从PHP返回到jQuery ajax调用

您可以通过更改php中的http响应代码来触发jQueryerror handling程序。 任何4xx或5xx错误都应该有效,但最好留在rfc。

PHP

 // no output before the header()-call header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error'); echo "foo"; 

jQuery

 [...] error: function(jqxhr) { alert(jqxhr.responseText) } [...] 

问题是,如果你的php响应,那么它在技术上不是错误,必须在成功回调中处理。

 $.ajax({ url: $(this).attr("action"), context: document.body, data: formData, type: "POST", contentType: false, processData: false, success: function(data){ alert('The response is: '+data); if(data=="empty sting"){ alert("The string is empty"); } else if (data == 'foo') { alert("The string equals 'foo'"); } else { alert("It works"); } }, }); 

在你的PHP中:

 if (myString == '') { echo('empty string'); } else if (myString == 'foo') { echo('foo'); } 

当调用在发送过程中失败时,将触发ajax方法的“错误”设置。 像“超时”,“404”等错误……

如果要控制服务器的某些响应,可以在“成功”设置中编写此代码。

 $.ajax({ url: $(this).attr("action"), context: document.body, data: formData, type: "POST", contentType: false, processData: false, success: function(response){ if (response == '') { // Send "stringIsEmpty" error to ajax } else if (response== 'foo') { // Send "stringEqualsFoo" error to ajax } } } 

});

PHP可能是这样的

 if (myString == '') { echo ''; } else if (myString == 'foo') { echo 'foo'; } 

因此,如果返回的字符串为空,或者它等于“foo”,您可能认为这是一个错误,但HTTP认为它是成功的,您需要在“success”函数中查找这些字符串。