JS請求報錯:Unexpected token T in JSON at position 0

來源:互聯網
上載者:User

標籤:表示   驗證   require   als   sha   let   ken   date   提交   

<?php /*最近做一個ajax validate表單驗證提交的代碼,在ajax提交的時候JS請求報錯:Unexpected token T in JSON at position 0描述:這是一個請求,參數的地方報這要的錯,請問是為什嗎?百思不得其解啊解決方案1:去看下json的格式要求,JSON.parse()方法對json串要求很嚴格解決方案2:http://www.bejson.com/用這個網址測試試試json格式是不是有誤,換行,單引號什麼的會導致報錯,格式要求很嚴格以下會導致報錯:JSON字串裡的非數字型索引值沒有雙引號JSON中存在t這樣的定位字元,看起來和空格一樣,但是就是因為它的存在校正不通過。去掉就能過了。編輯器有bom頭也會造成解決方案3:某個地方用了JSON.parse,但是傳入的參數不是一個合格的json字串。以上介紹了“ (javascript)JS請求報錯:Unexpected token T in JSON at position 0”的問題解答,希望對有需要的網友有所協助。*//***js ajax發送代碼*/ $.validator.setDefaults({    submitHandler: function() {      // alert("提交事件!");return false;       var query=new Object();        query.user_name=$(‘#user_name‘).val();        query.sex=$(‘#sex‘).val();        query.country=$(‘#country‘).val();        query.email=$(‘#email‘).val();        query.mobile=$(‘#mobile‘).val();        query.weixin=$(‘#weixin‘).val();        query.address=$(‘#address‘).val();        query.messages=$(‘#messages‘).val();        var url="/controller/apply.php";        $.ajax({            url:url,            type:"POST",            data:query,            dataType:"json",            success:function(res){                if(res.status==1){                    alert(res.msg);                    window.location.replace("http://www.fxbob.com/a/zhongwen/hezuohuoban/20170620/21.html");                }else{                    alert(res.msg);                }            },            error: function(XMLHttpRequest, textStatus, errorThrown) {            console.log(XMLHttpRequest);             // console.log(errorThrown); alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus);},complete: function(XMLHttpRequest, textStatus) { this; // 調用本次AJAX請求時傳遞的options參數}        });        return false;    }});/***php的ajax接收代碼Apply.php如下:*/<?php // header("Content-type: text/html; charset=utf-8");define(‘DEDEMOB‘, ‘Y‘);require_once(dirname(__FILE__)."/../include/common.inc.php");//載入郵件類require_once(dirname(__FILE__)."/../include/Smtp.class.php");if($_POST){$data=$_POST;$str=<<<EOT<html>        <head>        <meta http-equiv="Content-Type" content="text/html; charset=utf8" />        <title>Untitled Document</title>        </head>        <body>       <p>全名:{$data[‘user_name‘]}</p>       <p>性別:{$data[‘sex‘]}</p>       <p>國家:{$data[‘country‘]}</p>       <p>地址:{$data[‘address‘]}</p>       <p>郵箱:{$data[‘email‘]}</p>       <p>手機:{$data[‘mobile‘]}</p>       <p>:{$data[‘weixin‘]}</p>       <p>留言:{$data[‘messages‘]}</p>        </body>    </html>EOT;//[email protected]//******************** 配置資訊 ********************************$smtpserver = "smtp.163.com";//SMTP伺服器$smtpserverport =25;//SMTP伺服器連接埠$smtpusermail = "[email protected]";//SMTP伺服器的使用者郵箱$smtpemailto = "[email protected]";//發送給誰$smtpuser = "[email protected]";//SMTP伺服器的使用者帳號,註:部分郵箱只需@前面的使用者名稱$smtppass = "rainliu213";//SMTP伺服器的使用者密碼$mailtitle = "申請經紀人";//郵件主題$mailcontent = $str;//郵件內容$mailtype = "HTML";//郵件格式(HTML/TXT),TXT為文本郵件//************************ 配置資訊 ****************************$smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的一個true是表示使用身分識別驗證,否則不使用身分識別驗證.// $smtp->debug = true;//是否顯示發送的調試資訊$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);if($state){$res[‘status‘]=1;$res[‘msg‘]="提交成功,請等待申核";}else{$res[‘status‘]=0;$res[‘msg‘]="網路忙,提交失敗,請稍候再嘗試";}echo  json_encode($res);exit;}/***最後在網上查了好多這方面的錯誤,排查到是這個apply.php檔案帶bom頭導致json解析錯誤導致引起了,一個小小json排查錯誤花了三個小時時間。無語了*/

 

JS請求報錯:Unexpected token T in JSON at position 0

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.