Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

javascript - How do i consuming rest service with jquery in phonegap

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...