ajax json返回syntaxerror:意外令牌,努力弄清楚它几天

时间:2013-06-23 08:01:34

标签: ajax json parse-error

使用jQuery,php,mysql,我编写了程序 得到ueprice:

(1)成功后获得价格     $ prices = array();
        $行=和mysql_fetch_row($ RES);
        $ ueprice = intval($ row [0]);
        $ pricetotal = $ ueprice * $ suryou;
    $ selprice = round($ pricetotal * $ crate);
    $ prices = array(
    'unitp'=> $ ueprice,
    'sump'=> $ pricetotal,
    'sellp'=> $ selprice
        );
(2)使用json_encode
返回$ price      标题('Content-Type:application / json');
     echo json_encode($ prices);
     出口;

(3)调用php的javascript是:
    $(函数(){
    setPrices1d();
    $(“#selectQuantity1d”)。on(“change”,setPrices1d);
    });

 function setPrices1d() {   

$(document).ready(function()     
{    

     var grn = $('#main1d').val();      //get rank    
     var ghn = $('#sub1d option:selected').text();    //get hinban    
     var gkn = $('#detail1d').val();     //get kinou    
     var gch = $('#curtainH1d').val();     //get curtain height    
     var gcw = $('#curtainW1d').val();     //get curtain width    
     var gcr = $('#sellingRate1d').val();     //get customer_rate   
     var gsu = $('#selectQuantity1d').val();     //get suryou   

             var q = encodeURIComponent("drape");    
             var url = 'jsonprice.php?rank=' + grn + '&categ=' + q + '&hinban=' 
                        + ghn +        '&kinou=' + gkn + '&curtainh=' + gch +    
                        '&curtainw=' + gcw + '&crate=' + gcr + '&suryou=' + gsu;    
         $.ajax({       
         type: "GET",        
         url: url,    

         dataType: "json",    
        /**  
          * Ajax通信が成功した場合に呼び出されるメソッド    
          */  
         success: function(data) {myObject=JSON.parse(data);}    
        {    
             //結果が0件の場合    
             if(data == null) alert('データが0件でした');    
             //返ってきたデータの表示    

             var $unitprice = $('#unitPrice1d');    
             var $sumprice =  $('#sumTotal1d');    
            var $sellprice = $('#sellingPrice1d');    
             // clear input area  
            $unitprice.empty();    
            $sumprice.empty();    
            $sellprice.empty();    
            //set prices into text area   

            $unitprice.val(data[0].unitp);    
            $sumprice.val(data[0].sump);    
            $sellprice.val(data[0].sellp);

                 },    

        /**    
         * Ajax通信が失敗場合に呼び出されるメソッド   
         */
        error: function(XMLHttpRequest, textStatus, errorThrown)   
        {   
         alert('エラー : HttpReq= ' + XMLHttpRequest + ' textStatus =' +  textStatus + ' errThrown =' + errorThrown + ' errThrown.message  =' + errorThrown.message + ' HttpReq.status =' + XMLHttpRequest.status);    
        }   
     });      
 });      

}

(4)$ .ajax以上返回以下错误:     HttpReq = [对象]     errThrown = SyntaxError:意外的令牌a     Httpreq.status = 200

(5)要弄清楚这个错误,我花了几天时间,但还没有解决。  使用谷歌浏览器, 网络响应在我看来很好: 网络响应  array(8){    [ “秩”] =>
   string(1)“S”    [ “CATEG”] =>    string(12)“drape”    [ “hinban”] =>    字符串(7)“TKR8001”    [ “kinou”] =>    string(10)“2倍ヒダ”    [ “curtainh”] =>    string(6)“45-120”    [ “curtainw”] =>    string(5)“50-75”     [ “箱”] =>    string(4)“0.90”    [ “suryou”] =>    string(1)“3”  }

{ “UNITP”:30000, “储槽”:90000 “sellp”:81000}

我慷慨地批评你对这个解析错误的建议, 非常感谢!

1 个答案:

答案 0 :(得分:0)

我找到了错误原因(语法错误:意外令牌),所以我想描述原因。

(1)错误发生在jsonprice.php的编码中。  出于调试目的,在其中编写了以下两个代码。  打印“
sql = $ sql”
 var_dump($ array)

当我注释掉这两个编码时,语法错误停止了。

(2)在评论出这些编纂之后,  我又得到了一个错误:
 (未捕获的TypeError:无法读取未定义的属性'unitp')

此错误的原因是数组描述。  $ prices = array  $ prices [] = array< - []是必需的

之后,我得到了正确的结果。