简体   繁体   English

从带有ajax调用的REST服务的js方法返回值

[英]returning value from js method with ajax call to REST service

What I am trying to do is call a js method that uses ajax to call a REST service and then return that response to the original call. 我想做的是调用一个js方法,该方法使用ajax调用REST服务,然后将该响应返回到原始调用。 Sounds simple enough, but I haven't been able to find a sufficient answer here on SO to get it to work. 听起来很简单,但是我无法在SO上找到足够的答案来使其正常工作。

If async is true, I get undefined. 如果async为true,则无法定义。 If I set async to false, it works, but it is holding up the loading of the page. 如果将async设置为false,它可以工作,但会阻止页面的加载。 I have tried several attempts with different examples of using promises and callbacks, but just haven't been able to figure it out. 我已经尝试了使用promise和回调的不同示例,但是还没有弄清楚。 I know that there is something very basic I am missing, or concept I am failing to grasp. 我知道我缺少一些非常基本的东西,或者是我未能掌握的概念。

In this example, I am wanting to check if an item is on a persons watch list for an auction. 在此示例中,我要检查某项是否在拍卖的人员监视列表中。 If it is, then it will set the icon class to active so it will change the icon color and they can see that it is on their watch list. 如果是,则它将图标类设置为活动状态,以便更改图标颜色,他们可以看到它在其监视列表中。 If it is not, it remains greyed out. 如果不是,它将保持为灰色。

Each page will have a few things like this example. 每个页面都会有一些类似此示例的内容。

Page1.php

jQuery(document).ready(function($){

var w = mgapp.checkWatchlist($id, localStorage.u_pcjid);
if(!w.error){
  $("#watchlist").removeClass('watchlist').addClass('watchlist-active');
}

});

The method is on the script.js page. 该方法在script.js页面上。 The data returned from the ajax call is a JSON object. 从ajax调用返回的数据是JSON对象。
NOTE: async is currently set to false just to get it to work. 注意:async当前设置为false只是为了使其工作。

script.js  

var mgapp = {

checkWatchlist: function($iid, $uid) {
    var $msg;
    $.ajax({
     url: 'https://somedomain/api/v1/watchlist/item/' + $iid + '/' + $uid,
     type: "GET",
     headers: {"Authorization": localStorage.u_apikey},
     contentType: 'application/json',
     async: false
  }) 
  .done(function(data){
      $msg = data;
  })
  .fail(function(data){
    $msg = data;
  });   
 return $msg;      
}

}

I am sure I will need to have a callback function or something, but none of the attempts I have made with those have worked. 我确定我将需要有回调函数之类的东西,但是我所做的任何尝试都没有成功。

Just to clarify. 只是为了澄清。 All the code is working as is, it is just not working asynchronously. 所有代码都按原样工作,而不是异步工作。 What I am wanting is to have async set to true so that i am making an actual asynchronous call so that this is not holding up the loading of the page. 我想要的是将async设置为true,以便进行实际的异步调用,这样就不会阻止页面的加载了。 I need the value returned from the ajax call available to the var on Page1.php. 我需要从Page1.php上的var可用的ajax调用返回的值。

Thanks in advance. 提前致谢。

If figured it out. 如果想通了。

    var w = mgapp.checkWatchlist($id, localStorage.u_pcjid).done(function() {
    if(!w.responseJSON.error){
      $("#watchlist").removeClass('watchlist').addClass('watchlist-active');
    }
})




  checkWatchlist: function($iid, $uid) {
    return $.ajax({
     url: 'https://somedomain.c9.io/api/v1/watchlist/item/' + $iid + '/' + $uid,
     type: "GET",
     headers: {"Authorization": localStorage.u_apikey},
     contentType: 'application/json'
  }) 
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM