我如何使用REST的服务与jQuery中的PhoneGap如何使用、REST、PhoneGap、jQuery

2023-09-10 21:56:28 作者:安徒生,你是个谎言家

我使用的Monaca IDE +的PhoneGap构建一个手机应用程序。

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

我创建了一个宁静的服务器 - http://engridmedia.com/next / API /渠道/用户/ ID / 1

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

和我试图消耗在我的js文件这个jQuery脚本的JSON REST服务。

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>

这应该不工作?我已经包括了jquery.min.js文件和AJAX文件的页面。但它只是不会呈东西。

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.

推荐答案

试试这个:

$(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.