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条)

报告相同问题?

悬赏问题

  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画
  • ¥15 TeleScan不能修改bar
  • ¥15 hadoop中启动hive报错如下怎么解决
  • ¥15 如何优化QWebEngineView 加载url的速度
  • ¥15 关于#hadoop#的问题,请各位专家解答!
  • ¥15 如何批量抓取网站信息
  • ¥15 Spring Boot离线人脸识别