douchi7073 2011-06-16 18:20
浏览 51
已采纳

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

I was wondering if I can return an error callback back to my jquery from my php page that I created, which will then display an alert based upon the actions that happen in my php page. I tried creating a header with a 404 error but that didn't seem to work.

Sample JQuery Code:

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

Sample PHP Code aka 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();
    }

     ?>

-EDIT-

If you get the invalid label message when using json this is what I did to fix this problem:

Server Side PHP Code Part-

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).')';
}

Client Side JQuery Part-

$('#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;
        });
  • 写回答

2条回答 默认 最新

  • douweida2878 2011-06-16 18:33
    关注

    You can a return a JSON response from your PHP with a success boolean.

    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));
    }
    

    and in your Javascript:

     $.ajax({
                type: 'GET',
                dataType: 'json',
                data: dataValues,
                url: 'http://somepage.php',
                success: function(response){
                     if(response.success) {
                       alert('Success');
                     }
                     else {
                       alert('Failure');
                     }
                }
            });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站