如何从 jQuery 的 $.get 方法中提取对象?
我正在尝试使用 jQuery 的 $.get 方法从回调中返回单个对象。
我可以轻松地显示整个回调,但无法从方法中选择单个对象
这是我的代码:
<script>
$.get("http://domain.com/marketplace/api/v0/random_business_json/?callback=mycallback",
function(data){
$('.result').html(data);
});
</script>
这是在我的浏览器中返回的回调:
mycallback([{"pk": 6484, "model": "business.business", "fields": {"point": "POINT (-122.5447999999999951 45.7806700000000006)", "fax": "360-687-3148", "validated": true, "meta_description": null, "city": "Battle Ground", "mailing_zip_code": null, "mailing_address2": null, "state": "WA", "mailing_address1": null, "extension2": null, "extension1": null, "hours_text": "Opens Thursday\n at 8:30 a.m.", "latitude": "45.780670", "thumbnail": null, "zip_code": "98604", "website": "", "suggested_type": "", "description": "", "phone2": "", "address1": "713 West Main Street", "address2": "Suite 101", "phone1": "687-3149", "default_hours": null, "nickname": "", "slug": "boyd-james-m", "categories": [1218, 1227], "additional_hours_info": "", "business_type": 6, "name": "Boyd, Gaffney, Sowards, Mc Cray, Treosti, PLLC", "created": "2010-05-12 22:52:38", "safe_description": "", "notes": "Owner: STEVEN SOWARDS\n\nCONTACT_NAME: STEVEN SOWARDS\nTITLE_DESC: \n", "pre_name": "", "modified": "2010-05-12 22:52:48", "longitude": "-122.544800", "email": "", "mailing_state": null, "mailing_city": null}}])
我希望能够提取诸如 pk、fields、ct 等部分。我
尝试将 $('.result').html(data); 替换为 $('.result').html(data.pk); 看看是否类似的事情会起作用,但没有成功。
任何帮助将不胜感激。
谢谢!
I am trying to return individual objects from a callback using jQuery's $.get method.
I can easily display the whole callback but can't pick individual objects from the method
Here's my code:
<script>
$.get("http://domain.com/marketplace/api/v0/random_business_json/?callback=mycallback",
function(data){
$('.result').html(data);
});
</script>
Here's the callback that is returned in my browser:
mycallback([{"pk": 6484, "model": "business.business", "fields": {"point": "POINT (-122.5447999999999951 45.7806700000000006)", "fax": "360-687-3148", "validated": true, "meta_description": null, "city": "Battle Ground", "mailing_zip_code": null, "mailing_address2": null, "state": "WA", "mailing_address1": null, "extension2": null, "extension1": null, "hours_text": "Opens Thursday\n at 8:30 a.m.", "latitude": "45.780670", "thumbnail": null, "zip_code": "98604", "website": "", "suggested_type": "", "description": "", "phone2": "", "address1": "713 West Main Street", "address2": "Suite 101", "phone1": "687-3149", "default_hours": null, "nickname": "", "slug": "boyd-james-m", "categories": [1218, 1227], "additional_hours_info": "", "business_type": 6, "name": "Boyd, Gaffney, Sowards, Mc Cray, Treosti, PLLC", "created": "2010-05-12 22:52:38", "safe_description": "", "notes": "Owner: STEVEN SOWARDS\n\nCONTACT_NAME: STEVEN SOWARDS\nTITLE_DESC: \n", "pre_name": "", "modified": "2010-05-12 22:52:48", "longitude": "-122.544800", "email": "", "mailing_state": null, "mailing_city": null}}])
I want to be able to pull pieces out like the pk,fields, ct etc...
I tried replacing $('.result').html(data); with $('.result').html(data.pk); to see if something like that would work but had no success.
Any help would be appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果响应是 JSON,则成功回调的
data
参数是一个对象。尝试将其解析为 HTML 是没有意义的:The
data
parameter to your success callback is an object if the response is JSON. Trying to parse it as HTML doesn't make sense:尝试
$('.result').html(data[0].pk);
try
$('.result').html(data[0].pk);
jQuery.getJSON()
具有 JSONP 支持。这可能就是您正在寻找的。在 URL 中使用
?callback=?
而不是?callback=mycallback
来使其正常工作。http://api.jquery.com/jQuery.getJSON/
jQuery.getJSON()
has JSONP support. This might be what you're looking for.Use
?callback=?
in the URL instead of?callback=mycallback
to get it working.http://api.jquery.com/jQuery.getJSON/