简体   繁体   English

从服务器获取JSON响应,但无法通过Jquery显示为HTML

[英]Getting JSON response from the server but can't display as HTML with Jquery

I'm building a mobile app and i build a PHP API that renders data from a mysql db and encode it as JSON 我正在构建一个移动应用程序,并构建了一个PHP API,该API可以渲染mysql数据库中的数据并将其编码为JSON。

i call this api with Jquery ajax to get to display the records from the json output as JSONP and render it in my document in the success function. 我使用Jquery ajax调用此api,以将JSON输出中的记录显示为JSONP,并在成功函数中将其呈现在我的文档中。

so far i get the JSON correctly when i do the request via $.ajax and i can see the data in the Response in my firebug but in the other side the ajax function fires the ERROR not the Success. 到目前为止,当我通过$ .ajax执行请求时,我可以正确获取JSON,并且可以在Firebug中的Response中看到数据,但在另一端,ajax函数会引发错误而不是成功。

i have a demo code in here : jsfidle 我在这里有一个演示代码: jsfidle

this is my $.ajax Call: 这是我的$ .ajax电话:

$(document).on("pageinit","#myPage", function() {   
    $("#autocomplete").on("listviewbeforefilter", function ( e, data ) {
        var $ul = $( this ),
            $input = $( data.input ),
            value = $input.val(),
            html = "";
            $ul.html( "" );            
            var dataString = 'keyword='+value;
        if (value.length > 2 ) {            
            $.mobile.loading("show");                       
            $.ajax({
                type: "GET",
                url: "http://example.com/search.php",
                dataType: "jsonp",
                jsonpCallback: 'jsonpCallback',               
                cache:true,             
                data: dataString,
                success: function(data) {               
                $.mobile.loading("hide");                                       
                alert(data);                     
                }
            })         
        }
    });
});

if you check the net panel you 'll find it successful and there is data coming. 如果您检查了网络面板,您会发现它成功了,并且有数据即将到来。

Kindly Advise. 好心提醒。 Thanks A lot 非常感谢

您的响应未在响应中包含回调jsonpCallback(),我看到的响应只是({"name": ... })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM