如何在php中返回错误回调?

如何在php中返回错误回调?,php,javascript,jquery,callback,http-status-code-404,Php,Javascript,Jquery,Callback,Http Status Code 404,我想知道是否可以从我创建的php页面返回一个错误回调到jquery,然后根据php页面中发生的操作显示一个警报。我试图创建一个带有404错误的标题,但似乎不起作用 JQuery代码示例: $(document).ready(function() { var messageid= '12233122abdbc'; var url = 'https://mail.google.com/mail/u/0/#all/' + messageid; v

我想知道是否可以从我创建的php页面返回一个错误回调到jquery,然后根据php页面中发生的操作显示一个警报。我试图创建一个带有404错误的标题,但似乎不起作用

JQuery代码示例:

$(document).ready(function()
    {
        var messageid= '12233122abdbc';
        var url = 'https://mail.google.com/mail/u/0/#all/' + messageid;
        var encodedurl = encodeURIComponent(url);
        var emailSubject = 'Testing123';
        var fromName = 'email@emailtest.com';

        var dataValues = "subject=" + emailSubject + "&url=" + encodedurl + "&from=" + fromName + "&messageID=" + messageid;
        $('#myForm').submit(function(){
            $.ajax({
                type: 'GET',
                data: dataValues,
                url: 'http://somepage.php',
                success: function(){
                     alert('It Was Sent')
                }
                error: function() {
                    alert('ERROR - MessageID Duplicate')
                }
            });
            return false;
        });
    });
PHP代码示例(也称为somepage.PHP):

<?php
include_once('test1.php');
include_once('test2.php');


if(isset($_GET['subject']))
{
   $subject=$_GET['subject'];
}
else
{
   $subject="";
}

if(isset($_GET['url']))
{
   $url=$_GET['url'];
}
else
{
   $url="";
}
if(isset($_GET['from']))
{
    $from=$_GET['from'];
}
else
{
    $from="";
}

if(isset($_GET['messageID']))
{
    $messageID = $_GET['messageID'];
}
else
{
    $messageID="";
}



    $stoqbq = new test2($from, $messageID);
    $msgID = new stoqbq->getMessageID();
    if($msgID = $messageID)
    {
        header("HTTP/1.0 404 Not Found");
        exit;
    }
    else
    {
        $userID = $stoqbq->getUser();
        $stoqb = new test1($subject,$url,$messageID,$userID);
        $stoqb->sendtoquickbase();
    }

     ?>
客户端JQuery部分-

$('#myForm').submit(function(){
            $.ajax({
                type: 'GET',
                data: dataValues,
                cache: false,
                contentType: "application/json",
                dataType: "json",
                url: "http://somepage.php?&callback=?",
                success: function(response){
                    alert(response.success);
                }
            });
            return false;
        });

对于相同的问题,有一个可接受的q/a:

基本上,您需要从错误回调函数中获取响应消息:

$('#myForm').submit(function(){
    $.ajax({
        type: 'GET',
        data: dataValues,
        url: 'http://somepage.php',
        success: function(){
             alert('It Was Sent')
        }
        error: function(xhr, status, error) {
      alert(xhr.responseText);
    }
    });
    return false;
});

您可以使用一个成功的布尔值从PHP返回JSON响应

if($msgID = $messageID)
{
    echo json_encode(array('success' => false));
}
else
{
    $userID = $stoqbq->getUser();
    $stoqb = new test1($subject,$url,$messageID,$userID);
    $stoqb->sendtoquickbase();
    echo json_encode(array('success' => true));
}
在Javascript中:

 $.ajax({
            type: 'GET',
            dataType: 'json',
            data: dataValues,
            url: 'http://somepage.php',
            success: function(response){
                 if(response.success) {
                   alert('Success');
                 }
                 else {
                   alert('Failure');
                 }
            }
        });

什么是回应?邮件发送成功?哦,这看起来像是我在寻找的。谢谢。请看一下被接受的问答:)嗯,这看起来像是一个更简单的想法。请看一下:)嗯,它不起作用,即使它在解决方案上是有意义的。你有没有想到为什么警报不会为我弹出?使用Firebug或谷歌代码检查器查看服务器根据AJAX请求返回的内容。每次发出AJAX请求时,您都应该能够检查响应。在Firebug中,它将在您的控制台中弹出。如果您使用的是Chrome,它将显示在代码检查器的“网络”选项卡下;我得到的响应为空,这在firefox firebug if(response.success)中显示
 $.ajax({
            type: 'GET',
            dataType: 'json',
            data: dataValues,
            url: 'http://somepage.php',
            success: function(response){
                 if(response.success) {
                   alert('Success');
                 }
                 else {
                   alert('Failure');
                 }
            }
        });