简体   繁体   English

jQuery - 为什么总是调用 Ajax() 错误回调?

[英]jQuery - Why is the Ajax() error callback always called?

I am trying to return a confirmation JSON object back to my AJAX function.我正在尝试将确认 JSON object 返回给我的 AJAX ZC1C425268E68385D1AB5074F66Z。 For some reason, even though, the post is successful (200) the error callback function is always called.出于某种原因,即使发布成功(200)错误回调 function 总是被调用。 I am logging the returning JSON to a file for dubgging and it appears correct.我正在将返回的 JSON 记录到一个文件中进行配音,它看起来是正确的。 I cannot figure out why this is happening.我无法弄清楚为什么会这样。 Can someone offer a suggestion?有人可以提供建议吗?

PHP Controller Action (CI): PHP Controller 动作(CI):

public function sendMail()
    {
        $senderName = trim($_POST['senderName']);
        $returnEmail = trim($_POST['returnEmail']);
        $message = trim($_POST['message']);


        if (valid_email($returnEmail))
        {
            send_email('email@email.com','Website Email From: '.$senderName, $message);
            $success = array('success'=>'Mail Sent');

            //Debugging to file
            $myFile = "testFile.txt";
            $fh = fopen($myFile, 'w') or die("can't open file");
            $stringData = json_encode($success);
            fwrite($fh, $stringData);
            fclose($fh);


            echo json_encode($success);
        }
        else
        {
            $errorMessage = array('error'=>'Invalid Email Address');
            echo json_encode($errorMessage);
        }
    }

} }

JS: JS:

$.ajax({
                   type: "POST",
                   url: "http://domain.com/index.php/mail/sendmail",
                   data: {senderName: senderName, returnEmail: senderAddr, message: message },
                   dataType: "JSON",
                   success: function(msg){
                     console.log(msg);
                   },
                   error: function(data){
                        alert("Something went wrong"); // possible that JSON wasn't returned
                    }
                 });

The problem was that I was not using a relative url for a target.问题是我没有使用相对的 url 作为目标。 I believe the issue was a cross domain scripting problem.我相信这个问题是一个跨域脚本问题。 I changed the url property to index.php/mail/sendmail and all is well.我将 url 属性更改为 index.php/mail/sendmail,一切都很好。

$.ajax({
               type: "POST",
               url: "index.php/mail/sendmail",
               data: {senderName: senderName, returnEmail: senderAddr, message: message },
               dataType: "JSON",
               success: function(msg){
                 console.log(msg);
               },
               error:function (xhr, ajaxOptions, thrownError){
                    var x = xhr;
                    var y = ajaxOptions;
                    var z = thrownError;
                }
             });

The error callback takes up to three arguments: the XHR object, an error string, and an optional exception object. error回调最多占用三个arguments:XHR object、错误字符串和可选异常 object。 Accept the last two as well and they should tell you what's going on.接受最后两个,他们应该告诉你发生了什么。

You may also want to use a debugger like Firebug, Dragonfly, or Chrome's developer tools to see if the request is as successful as you think.您可能还想使用诸如 Firebug、Dragonfly 或 Chrome 的开发人员工具之类的调试器来查看请求是否如您想象的那样成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM