How do i consuming rest service with jquery in phonegap

时间:2015-06-30 13:38:30

标签: javascript jquery ajax rest monaca

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 个答案:

答案 0 :(得分:1)

试试这个:

$(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);
});
});

您正在数组中返回对象。您需要在该数组中获取第一个对象。