问题
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