将错误消息从php发送到ajax

Hor*_*ray 3 javascript php ajax jquery

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

PHP:

if (myString == '') {
    // Send "stringIsEmpty" error to ajax
} else if (myString == 'foo') {
    // Send "stringEqualsFoo" error to ajax
}
Run Code Online (Sandbox Code Playgroud)

阿贾克斯

$.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");
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

如何向ajax发送错误消息?

更新

这是我的php文件.我尝试使用echo解决方案答案说,但当我输出的data是(在ajax中),我得到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");
        } 
    }
?>
Run Code Online (Sandbox Code Playgroud)

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

Tim*_*mon 5

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

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

success: function(data){
        if (data === "stringIsEmpty") {
           triggerError("stringIsEmpty");
        } else if (data === "stringEqualsFoo") {
           triggerError("stringEqualsFoo");
        }
    },
Run Code Online (Sandbox Code Playgroud)

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

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

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

// Create a function to see if the string is empty
$funcOutput = isStringEmpty();
echo $funcOutput;
Run Code Online (Sandbox Code Playgroud)

或者特别为例子:

echo "stringIsEmpty";
Run Code Online (Sandbox Code Playgroud)

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


小智 5

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

PHP

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

jQuery

[...]
error: function(jqxhr) {
    alert(jqxhr.responseText)
}
[...]
Run Code Online (Sandbox Code Playgroud)