从XmlHttpRequest.responseJSON解析JSON

编程入门 行业动态 更新时间:2024-10-22 18:41:55
本文介绍了从XmlHttpRequest.responseJSON解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通过XmlHttpRequest得到了JSON。

var req = new XMLHttpRequest; req.overrideMimeType(application / json); req.open('GET',BITLY_CREATE_API + encodeURIComponent(url) + BITLY_API_LOGIN,true); var target = this; req.onload = function(){ta​​rget.parseJSON(req,url)}; req.send(null); parseJSON:function(req,url){ if(req.status == 200){ var jsonResponse = req.responseJSON; var bitlyUrl = jsonResponse.results [url] .shortUrl; }

我在firefox插件中这样做。当我运行时,我得到的错误jsonResponse是未定义的行 var bitlyUrl = jsonResponse.results [url] .shortUrl; 。我在这里解析JSON时做错了什么?或者这个代码有什么问题?

解决方案

标准的XMLHttpRequest没有 responseJSON 属性,只需 responseText 和 responseXML 。只要有点真正的响应你的请求的一些JSON, responseText 应该包含JSON代码作为文本,所以你所要做的就是解析它: p>

var jsonResponse = JSON.parse(req.responseText);

如果您喜欢使用 responseJSON ,但想要比JQuery更轻量级的解决方案,你可能想看看我的JSONHttpRequest。它的工作方式与普通的XMLHttpRequest完全相同,但也提供了 responseJSON 属性。所有你必须改变你的代码将是第一行:

var req = new JSONHttpRequest();

JSONHttpRequest还提供了将JavaScript对象作为JSON轻松发送的功能。更多细节和代码可以在我的博客中找到: http:// pixelsvsbytes/2011/12/teach-your-xmlhttprequest-some-json/ 。

PS:我知道链接到我自己的博客在这里不会被赞赏,但我认为我的脚本是一个很好的解决方案,所以我贴了它。请留下评论,如果你想我删除这个职位,我会这样做。

I'm trying to parse a bit.ly JSON response in javscript.

I get the JSON via XmlHttpRequest.

var req = new XMLHttpRequest; req.overrideMimeType("application/json"); req.open('GET', BITLY_CREATE_API + encodeURIComponent(url) + BITLY_API_LOGIN, true); var target = this; req.onload = function() {target.parseJSON(req, url)}; req.send(null); parseJSON: function(req, url) { if (req.status == 200) { var jsonResponse = req.responseJSON; var bitlyUrl = jsonResponse.results[url].shortUrl; }

I do this in a firefox addon. When I run I get the error "jsonResponse is undefined" for the line var bitlyUrl = jsonResponse.results[url].shortUrl;. Am I doing anything wrong in parsing JSON here? Or what is wrong with this code?

解决方案

The standard XMLHttpRequest has no responseJSON property, just responseText and responseXML. As long as bitly really responds with some JSON to your request, responseText should contain the JSON code as text, so all you've got to do is to parse it:

var jsonResponse = JSON.parse(req.responseText);

If you prefer to use responseJSON, but want a more lightweight solution than JQuery, you might want to check out my JSONHttpRequest. It works exactly like a normal XMLHttpRequest, but also provides the responseJSON property. All you have to change in your code would be the first line:

var req = new JSONHttpRequest();

JSONHttpRequest also provides functionality to easily send JavaScript objects as JSON. More details and the code can be found in my blog: pixelsvsbytes/2011/12/teach-your-xmlhttprequest-some-json/.

PS: I'm aware that linking to my own blog won't be appreciated here, but I think my script is a good solution to the problem, so I posted it anyway. Please leave a comment if you want me to remove this post and I'll do so.

更多推荐

从XmlHttpRequest.responseJSON解析JSON

本文发布于:2023-11-26 21:00:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XmlHttpRequest   responseJSON   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!