weixin_33709609 2015-04-28 09:45 采纳率: 0%
浏览 26

在jQuery中获取解析错误

I am trying to send data to server and get back some data in json format using ajax, what i've tried

if (!empty($_REQUEST['data1'])) {
       $dt = $_REQUEST['data1'];
       $arr = [1,2,3];
       header("Content-Type: application/json", true);
       echo json_encode($arr);
 }
<script>
        jQuery(document).ready(function(){
             jQuery('.selectpicker').selectpicker();
             jQuery('#datepick').datepicker({
                    format: "dd/mm/yyyy"
             });
             jQuery('.client_list a').click(function() {
                alert(jQuery(this).text()); 
                var data={};
                data['client_name']=jQuery(this).text();
                load_data(data,window.location.pathname);
             });
         });
        var load_data=function(data,url,selector){
            jQuery.post(url,{'data1':data})
            .done(function(result){
                console.log(result+"Hello");
            })
            .fail(function( jqXHR, textStatus) {
                console.log( "failed due:"+ textStatus);
            });
        };
    </script>

I'm getting parseerror why is is this

UPDATE
my php script and jquery are in one file and my url is window.location.pathname inside getJSON

  • 写回答

2条回答 默认 最新

  • DragonWar% 2015-04-28 09:46
    关注

    When jQuery receives a JSON response from an AJAX request it will automatically deserialise it for you. The error you're seeing is because you're calling parseJSON on an object, you just need to remove that call:

    jQuery.getJSON(url,{'data1':data})
        .done(function(result){
            console.log(result + "Hello");
        })
        .fail(function( jqXHR, textStatus) {
            console.log( "failed due:"+ textStatus);
        });
    
    评论

报告相同问题?