如何从 jQuery 的 $.get 方法中提取对象?

发布于 2024-11-01 17:26:00 字数 1645 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哭了丶谁疼 2024-11-08 17:26:00

如果响应是 JSON,则成功回调的 data 参数一个对象。尝试将其解析为 HTML 是没有意义的:

function(data) {
    alert("pk = " + data[0].pk);
}

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:

function(data) {
    alert("pk = " + data[0].pk);
}
高跟鞋的旋律 2024-11-08 17:26:00

尝试 $('.result').html(data[0].pk);

try $('.result').html(data[0].pk);

安静 2024-11-08 17:26:00

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文