dregvw1801
2013-12-12 00:57
浏览 294

echo json_encode()无法通过ajax调用工作

I really don't know what i'm missing here. I have this script:

        <script type="text/javascript">
            function ServiceOffer(){
                var service_name  = $('#service_name').val(),
                    dataString = "service="  + service_name;
                    $.ajax({
                        type: "POST",
                        url:  "posted.php",
                        data:dataString,
                        success:function(data){
                            console.log(data);
                        }
                    });
            }

            $(document).ready(function(){   
                ServiceOffer();

                $(document).on('change','#service_name',function(){
                    ServiceOffer();                 
                });     
            });
        </script>

And here is my code for posted.php

    <?php

        $connect = mysql_connect('localhost','root','') or die('Unable to connect'.mysql_error());
        $select = mysql_select_db('hcs',$connect) or die('Unable to select database'.mysql_error());
        $service = $_POST['service'];

        $query = mysql_query("SELECT * FROM serviceoffer WHERE servicename = '$service'");
        $num = mysql_num_rows($query);  

        while($rows = mysql_fetch_array($query)){
            $data[] = $rows;
        }
        echo json_encode($data);

So what i'm missing? I don't think there is a string that is being attached in my code and it gives me string in my console and not json encoded data. Please Help! Thanks!

Edit: Here is the data that is being returned in my console.

enter image description here

图片转代码服务由CSDN问答提供 功能建议

我真的不知道我在这里缺少什么。 我有这个脚本:

 &lt; script type =“text / javascript”&gt; 
 function ServiceOffer(){
 var service_name = $('#service_name')  .val(),
 dataString =“service =”+ service_name; 
 $ .ajax({
 type:“POST”,
 url:“posted.php”,
 data:dataString,
 success  :function(data){
 console.log(data); 
} 
}); 
} 
 
 $(document).ready(function(){
 ServiceOffer(); 
 \  n $(document).on('change','#service_name',function(){
 ServiceOffer(); 
}); 
}); 
&lt; / script&gt; 
    
 
 

这是我的代码 posted.php

 &lt;?php 
 
 $  connect = mysql_connect('localhost','root','') 或死('无法连接'.mysql_error()); 
 $ select = mysql_select_db('hcs',$ connect)或die('无法选择数据库'.mysql_error()); 
 $ service = $ _POST  ['service']; 
 
 $ query = mysql_query(“SELECT * FROM serviceoffer WHERE servicename ='$ service'”); 
 $ num = mysql_num_rows($ query);  
 
 while($ rows = mysql_fetch_array($ query)){
 $ data [] = $ rows; 
} 
 echo json_encode($ data); 
   
  
 

那么我错过了什么? 我不认为我的代码中附加了一个字符串,它在我的控制台中提供了字符串而不是json编码数据。 请帮忙! 谢谢!

编辑:这是我的控制台中返回的数据。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drrvnbwle80177811 2013-12-12 01:02
    已采纳

    You have three options:

    1. Add dataType: 'json' to the options in $.ajax.
    2. Add header("Content-type: application/json"); to the PHP code.
    3. Use console.log($.parseJSON(data)) in the success function.

    Note that you shouldn't use option 3 if you've also used options 1 or 2. jQuery automatically parses the JSON in the first two cases, and you'll try to parse the result of that, which won't work.

    打赏 评论
  • dongzhiyan5693 2013-12-12 01:02

    At a minimum, you should send the appropriate header in PHP, eg

    header('Content-type: application/json');
    echo json_encode($data);
    exit;
    

    You can also set the dataType in jQuery's $.ajax method to specify the expected response type. I also find it easier to send request parameters as object literals (saves having to build URL encoded strings), eg

    $.ajax({
        type: 'POST',
        dataType: 'json',
        data: { service: service_name },
        //etc
    

    Update

    As shirejedi mentioned, you should initialise $data as an array

    $data = array();
    while($rows = mysql_fetch_array($query)){
        $data[] = $rows;
    }
    
    打赏 评论

相关推荐 更多相似问题