How do i consuming rest service with jquery in phonegap

让人想犯罪 __ 提交于 2020-01-17 12:55:37

问题


I am using monaca IDE + phonegap to build a phone app.

I have created a restful server - http://engridmedia.com/next/api/channel/user/id/1

And i am trying to consume the json rest service with this jquery script in my js file.

$(document).ready(function() {
$.ajax({
    url: "http://engridmedia.com/next/api/channel/user/id/1"
}).then(function(data) {
   $('.ch-name').append(data.ch_name);
   $('.ch_logo').append(data.ch_logo);
});

});

and calling it it in the body like this

    <div>
<p class="ch_logo"> </p>
<p class="ch_name"> </p>
</div>

should this not be working? I have included the jquery.min.js file and the ajax file to the page . but it just wont show a thing.


回答1:


Try this:

$(document).ready(function() {
  $.ajax({
    cache: false,
      url: "http://engridmedia.com/next/api/channel/user/id/1",
    type: 'GET',
    crossDomain: true,
    dataType: 'json',
    success: function() {
        alert("Success");
    },
    error: function() {
        alert('Failed!');
    },
}).then(function(data) {
    var result = data [0];
    console.log(result)
    $('.ch-name').append(result.ch_name);
    $('.ch-logo').append(result.ch_logo);
});
});

You are returning a object in a array. You need to get the first object in that array.



来源:https://stackoverflow.com/questions/31139840/how-do-i-consuming-rest-service-with-jquery-in-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!