【问题标题】:How to return an error callback in php?如何在php中返回错误回调?
【发布时间】:2011-09-16 15:04:26
【问题描述】:

我想知道是否可以从我创建的 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();
    }

     ?>

-编辑-

如果您在使用 json 时收到无效标签消息,这是我为解决此问题所做的:

服务器端 PHP 代码部分-

if($msgID == $messageID)
{
    $response["success"] = "Error: Message Already In Quickbase";
    echo $_GET['callback'].'('.json_encode($response).')';
}
else
{
    $userID = $stoqbq->getUser();
    $stoqb = new SendToQuickbase($subject,$url,$messageID,$userID);
    $stoqb->sendtoquickbase();
    $response["success"] = "Success: Sent To Quickbase";
    echo $_GET['callback'].'('.json_encode($response).')';
}

客户端 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;
        });

【问题讨论】:

  • 还有什么反应?消息发送成功了吗?

标签: php javascript jquery callback http-status-code-404


【解决方案1】:

您可以使用成功布尔值从 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 或 Google Code Inspector 查看服务器在 AJAX 请求中返回的内容。每次触发 AJAX 请求时,您都应该能够检查响应。在 Firebug 中,它将在您的控制台中弹出。如果您使用的是 Chrome,它将显示在代码检查器的“网络”选项卡下。
  • 另外,尝试将其添加到您的 PHP 代码中:header('Content-type: text/json');
  • 我得到一个响应是 null 这在 firefox firebug if(response.success) 中显示
【解决方案2】:

有一个接受的 q/a 具有相同的内容:How to get the jQuery $.ajax error response text?

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

$('#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;
});

【讨论】:

  • 哦,这看起来像我正在寻找的东西,谢谢也会看看接受的 q/a :)
猜你喜欢
  • 2015-12-25
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 2013-10-03
  • 1970-01-01
相关资源
最近更新 更多