- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试在 javascript 中解析一个 bit.ly 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() {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;
}
我在 Firefox 插件中执行此操作。当我运行时,var bitlyUrl = jsonResponse.results[url].shortUrl;
行出现错误“jsonResponse is undefined”。我在这里解析 JSON 有什么问题吗?或者这段代码有什么问题?
最佳答案
我的新方法:fetch
TL;DR只要您不必发送同步请求或支持旧浏览器,我会推荐这种方式。
只要您的请求是异步的,您就可以使用 Fetch API发送 HTTP 请求。提取 API 与 promises 一起使用,这是在 JavaScript 中处理异步工作流的好方法。使用这种方法,您可以使用 fetch()
发送请求并使用 ResponseBody.json()
来解析响应:
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(jsonResponse) {
// do something with jsonResponse
});
兼容性:IE11 以及 Edge 12 和 13 不支持 Fetch API。但是,有 polyfills .
新方式二:responseType
作为 Londeren写在 his answer ,较新的浏览器允许您使用 responseType
属性来定义响应的预期格式。然后可以通过 response
属性访问解析的响应数据:
var req = new XMLHttpRequest();
req.responseType = 'json';
req.open('GET', url, true);
req.onload = function() {
var jsonResponse = req.response;
// do something with jsonResponse
};
req.send(null);
兼容性:responseType = 'json'
不受 IE11 支持。
经典方式
标准的 XMLHttpRequest 没有 responseJSON
属性,只有 responseText
和 responseXML
。只要 bitly 真的用一些 JSON 响应您的请求,responseText
应该包含 JSON 代码作为文本,所以您所要做的就是用 JSON.parse() 解析它
:
var req = new XMLHttpRequest();
req.overrideMimeType("application/json");
req.open('GET', url, true);
req.onload = function() {
var jsonResponse = JSON.parse(req.responseText);
// do something with jsonResponse
};
req.send(null);
兼容性:这种方法应该适用于任何支持 XMLHttpRequest
和 JSON
的浏览器。
JSONHttpRequest
如果您更喜欢使用 responseJSON
,但想要一个比 JQuery 更轻量级的解决方案,您可能需要查看我的 JSONHttpRequest。它的工作方式与普通的 XMLHttpRequest 完全相同,但还提供了 responseJSON
属性。您只需更改代码中的第一行即可:
var req = new JSONHttpRequest();
JSONHttpRequest 还提供了以 JSON 格式轻松发送 JavaScript 对象的功能。更多细节和代码可以在这里找到:http://pixelsvsbytes.com/2011/12/teach-your-xmlhttprequest-some-json/ .
完全披露:我是 Pixels|Bytes 的所有者。我认为我的脚本是原始问题的一个很好的解决方案,但今天它已经过时了。我不建议再使用它了。
关于javascript - 从 XmlHttpRequest.responseJSON 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1973140/
我以 JSON 格式从 Python Flask 发送数据。我能够获取数据,并在本地托管页面 http://127.0.0.1:5000/ 上的检查器中使用 for 循环。但是,当我将相同的 Java
在我的 javascript 文件中,我注意到我的问题出现在 JS 文件的第 19、30 和 35 行。我可以让 responseJson 在 getUserRepos() 函数中登录到控制台,但是如
这是我从我的 API 获取 JSON 数据的代码。我想做的是在我的 Flatlist 中打印各种药物的名称,然后使该对象可点击,这样我就可以在下一页上显示有关它的更多信息。我可以正确接收 JSON 数
我正在使用 Alamofire 尝试使用 image shack API 将图像放在图像棚服务器上。我没有收到回复并收到错误: FAILURE: Error Domain=NSCocoaErrorDo
我想限制 12 个帖子,只显示 post_featured 键值为 1 的精选帖子 componentDidMount() { return fetch(ConfigApp.URL+'json/
我正忙于研究为 Swift 创建的 Alamofire 网络库。我遇到过 responseJSON 函数: public func responseJSON( queue queue:
我正在使用 AlamoFire 调用我的网络服务: ApiManager.manager.request(.GET, webServiceCallUrl, parameters: ["id": 12
我正在尝试在我的应用程序中全局处理所有 ajax 异常。 我有一个简单的验证异常,在 ajax post 请求发送到服务器后抛出。 if (string.IsNullOrEmpty(name)) {
我有这种方法可以联系我的 REST 网络服务。我想在连接失败时生成异常。Xcode 告诉我不能使用函数 throw VendingMachineError.InvalidSelection 因为 me
鉴于以下情况: $(function() { function getTopMod(e) { var requesturl = '/r/lol/about/moderators
我正在开发一个小型网络框架来运行 HCI 研究并遇到以下问题: 我有一个运行 express 的 Node 服务器,用于从 JSON 文件为我的本地主机数据提供服务。不是最好的数据库,但由于它是一个单
我将 sessionManager.request 与 .validate().responseJSON 一起使用 是否可以在将响应传递给 responseJSON 之前修改我从服务器获得的响应?我需
( swift 2.2,Alamofire 3.0) 我有一个相当复杂的错误/状态检查协议(protocol),我在我的许多 REST 调用之后应用它,我自然希望重新使用此代码。 Alamofire.
我有处理位置数据的代码,因此我可以提取可以匿名化数据的详细信息——例如,如果我的地点 API 说它是 _type:“Building”和 building:“Safeway”——我可以保存数据作为纬度
Alamofire.request(todoEndpoint) .responseJSON { response in switch response.resu
我正在使用 alamofire 上传图片。这是注册 API,您可以在其中使用个人资料照片进行注册(这是我必须上传的内容) 所以我的代码是这样的(我将用另一个代码替换 print(JSON);这只是为了
注意:我需要使用 ObjectMapper,而不是 AlamoFireObjectMapper。 如何不去JSON而是从responseJSON数据映射到User?以下是尝试从 responseJSO
我有以下代码来获取对评论列表的回复。 (1条评论多条回复) static func fetchCommentsAndTheirReplies(articleId: String, failure: (
我正在尝试在 javascript 中解析一个 bit.ly JSON 响应。 我通过 XmlHttpRequest 获取 JSON。 var req = new XMLHttpRequest; r
我是 Swift 新手,我正在尝试升级一些旧的 Swift 代码。我收到以下警告: 'responseJSON(queue:dataPreprocessor:emptyResponseCodes:em
我是一名优秀的程序员,十分优秀!