无法转换收到的jsonp数据以在jquery

时间:2017-07-24 10:59:31

标签: jquery json datatables jsonp

我的jquery使用jsonp从服务器获取json数据。 收到的数据如下。

"d":"[{"id":28,"first_name":"Examiner","last_name":"Exam","phone_no":"9876543210","email":"examiner@gmail.com","address":"Nashik","username":"examiner","password":"examiner","user_type":"examiner ","active":true},{"id":5,"first_name":"Jeevan","last_name":"Jadhav","phone_no":"9876543210","email":"jeevan@gmail.com","address":"nashik","username":"jeevan","password":"jeevan","user_type":"examiner ","active":true},{"id":40,"first_name":"Niraj","last_name":"Borwal","phone_no":"3453453450","email":"niraj@pointmatrix.com","address":"Nashik","username":"niraj","password":"niraj","user_type":"examiner ","active":true},{"id":26,"first_name":"sumit","last_name":"mogare","phone_no":"8675456876","email":"sumit1@gmail.com","address":"Nashik","username":"sumit","password":"mogare","user_type":"examiner ","active":true},{"id":53,"first_name":"aaa","last_name":"aaa","phone_no":"1234567890","email":"aaa@gmail.com","address":"aaa","username":"aaa","password":"aaa","user_type":"examiner ","active":true}]"

我需要将此数据转换为适合添加到datatable。但是当我使用$.parseJSON(response)时,jquery会出错 我认为这些数据是一系列对象。

如何将此数据转换为json,以便我可以将其用作" aaData"数据源的来源 我的jquery代码如下

$('#btnClick').on("click", function () {
        debugger;
        var dt = 'type=' + $('#txt').val();
        $.ajax({
            dataType: "jsonp",
            url: "http://localhost:6168/WcfService.svc/getData",
            type: "POST",
            jsonpCallback: "callback",
            data: dt,
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                debugger;
                fillTable(response);
                console.log(response);
                // var parsed = JSON.parse(response);
                var dat = response;
                var data = dat.split(':');
                console.log(data);
                // var parsed = $.parseJSON('[' + response + ']');
                // var dt = $.parseJSON(parsed);
                //console.log(parsed);
                $('#text').text("you entered : " + response);
            },
            error: function () {
                alert("error");
            }
        });
        return false;
    });

和fillTable函数如下

function fillTable(resp) {
        $('[id*="mytable"]').DataTable({
            "aaData": resp,
            "aoColumns": [
        { "d": "id" },
        { "d": "first_name" },
        { "d": "last_name" },
        { "d": "phone_no" },
        { "d": "email" },
        { "d": "address" },
        { "d": "username" },
        { "d": "password" },
        { "d": "user_type" },
        { "d": "isActive" },
        ]
        });
    }

提前致谢

0 个答案:

没有答案