Jquery / Ember对象迭代

时间:2015-08-03 21:40:00

标签: jquery json ember.js

我有一个用于显示工作职位的余烬应用程序。我试图根据IP位置过滤那些工作位置。

我使用geonames根据请求的纬度和经度搜索状态。

api request是在

中制作的
var state=StateAdapter.getState(location);

然后,我打印结果

 console.log(state);

我有这个返回的对象状态:

Object {readyState:1 }
abort: ( statusText )
always: ()
complete: ()
done: ()
error: ()
fail: ()
getAllResponseHeaders: ()
getResponseHeader: ( key )
overrideMimeType: ( type )
pipe: ( /* fnDone, fnFail, fnProgress */ )
progress: ()
promise: ( obj )
readyState: 4
responseJSON: Object
responseText: "{"geonames":     [{"adminCode2":"039","countryName":"Mexico","adminCode1":"19","lng":"-100.29265","adminName2":"Monterrey","fcodeName":"populated place","adminName3":"","distance":"1.07626","timezone":{"dstOffset":-5,"gmtOffset":-6,"timeZoneId":"America/Monterrey"},"adminName4":"","adminName5":"","name":"Norte de Monterrey","fcode":"PPL","geonameId":9072770,"asciiName":"Norte de Monterrey","lat":"25.63646","population":0,"adminName1":"Nuevo León","countryId":"3996063","adminId1":"3522542","fclName":"city, village,...","elevation":0,"countryCode":"MX","adminId2":"8582459","toponymName":"Norte de Monterrey","fcl":"P","continentCode":"NA"}]}"
setRequestHeader: ( name, value )
state: ()
status: 200
statusCode: ( map )
statusText: "OK"
success: ()
then: ( /* fnDone, fnFail, fnProgress */ )
__proto__: Object

但是当我这样做时:

$.each( state, function( key, value ) {
console.log("state["+key +"]" + value);
});

控制台不会打印readyState,responseJSON,responseText,status或statusText。我需要responseText结果来过滤我的模型。

我做错了什么?多谢你们。

1 个答案:

答案 0 :(得分:1)

API响应是异步的。这个例子可行。

$.ajax("http://api.geonames.org/findNearbyPlaceNameJSON", {
  dataType: 'jsonp',
  data: {
    lat: 25.631590600000003,
    lng: 100.3019204,
    username: "demo",
    style: "full"
  }
}).done(function(response){
  $.each(response.geonames, function(index, geoname){
    console.log(geoname);
  });      
});

您不能简单地返回ajax请求,它不会立即获得您的数据。相反,你会使用承诺或回调。因此,例如,如果你想用你可以做的承诺来包装它:

function geoNames(loc){
  console.log(loc);
  return $.ajax("http://api.geonames.org/findNearbyPlaceNameJSON", {
    dataType: 'jsonp',
    data: {
      lat: loc.lat,
      lng: loc.lng,
      username: "demo",
      style: "full"
    }
  })
}

然后:

var loc = {lat: 25.631590600000003, lng: -100.3019204};
geoNames(loc).done(function(response){
  $.each(response.geonames, function(index, geoname){
    $("body").append("<p>" + geoname + "</p>");
  });      
});

或者使用回调:

function geoNames(loc,  callback){
  console.log(loc);
  return $.ajax("http://api.geonames.org/findNearbyPlaceNameJSON", {
    dataType: 'jsonp',
    data: {
      lat: loc.lat,
      lng: loc.lng,
      username: "demo",
      style: "full"
    }
  }).done(callback)
}

然后:

var loc = {lat: 25.631590600000003, lng: -100.3019204};
geoNames(loc, function(response){
  $.each(response.geonames, function(index, geoname){
    $("body").append("<p>" + geoname + "</p>");
  });      
});

另一个完全独立的问题是您不应该使用location作为变量名称。 window.location是特定于浏览器的全局变量,并不总是可以覆盖。请改用loc