如何发出REST GET请求(带有身份验证)并在javascript中解析结果? [英] How to make a REST GET request (with authentication) and parse the result in javascript?

查看:157
本文介绍了如何发出REST GET请求(带有身份验证)并在javascript中解析结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我无法控制的情况,Javascript是我唯一可用的语言选项.我是一个初学者,甚至不确定我是否以推荐"的方式解决问题.

Due to circumstances beyond my control, Javascript is the only language option available for me. I'm a beginner and am not even sure if I'm approaching the problem in a "recommended" manner.

简而言之,客户已在线设置了MarkLogicDB服务器,并为我提供了只读访问权限.我可以使用 HTTP GET协议查询服务器,以返回必须解析的XML文档.我已经能够创建curl命令以返回所需的数据(下面的示例);

Simply put, a customer has setup a MarkLogicDB server online and has given me read-only access. I can query the server with the HTTP GET protocol to return an XML document that has to be parsed. I've been able to create a curl command to return the data I need (example below);

curl --anyauth --user USERNAME:PASSWORD \
  -X GET \
  http://test.com:8020/v1/documents?uri=/path/to/file.xml

以上返回请求的XML文件.有人可以告诉我如何将以上代码转换为javascript代码吗?另外,我将如何解析数据?假设我要从某个元素或属性获取所有信息.如何做到这一点?

The above returns the requested XML file. Can someone please show me how I could convert the above to javascript code? Additionally, how would I parse the data? Let's say I want to get all the info from a certain element or attribute. How can this be accomplished?

Java/.NET中,这对我来说是微不足道的,但是在阅读了大量有关Javascript的在线教程之后,我的头开始旋转.每个教程都讨论网络浏览器,但是我是在服务器环境上进行的( parse.com CloudCode ).不涉及任何UI或HTML.为了进行调试,我只读取了console.log()创建的日志.

This would be trivial for me to do in Java/.NET, but after reading plenty of online tutorials on Javascript, my head is spinning. Every tutorial talks about web-browsers, but I'm doing this on a server environment (The parse.com CloudCode). There isn't any UI or HTML involved. For debugging, I just read the logs created with console.log().

推荐答案

https://parse.com/docs/cloud_code_guide#networking 看起来很清楚.

Parse.Cloud.httpRequest({
  url: 'http://test.com:8020/v1/documents',
  params: {
    uri : '/path/to/file.xml'
  },
  success: function(httpResponse) {
    console.log(httpResponse.text);
  },
  error: function(httpResponse) {
    console.error('Request failed with response code ' + httpResponse.status);
  }
});

但是您还需要身份验证. Parse.Cloud.httpRequest文档未包含任何示例.如果您对该供应商有支持,请向供应商询问摘要身份验证.

But you'll also need authentication. The Parse.Cloud.httpRequest docs don't include any examples for that. If you have support with that vendor, ask the vendor about digest authentication.

如果遇到问题,可以尝试将userpassword添加到httpRequest参数中,然后看看会发生什么.如果该堆栈的开发人员遵循XMLHttpRequest约定,则可能会起作用.

If you're stuck you might try adding user and password to the httpRequest params and see what happens. It might work, if the developers of this stack followed the XMLHttpRequest convention.

无法获得供应商的支持和现有功能,您将必须使用JavaScript自己实现身份验证.这通过生成进入请求标头的字符串来工作.这些资源应有帮助:

Failing support from the vendor and existing functionality, you'll have to implement authentication yourself, in JavaScript. This works by generating strings that go into the request headers. These resources should help:

  • http://en.wikipedia.org/wiki/Digest_access_authentication
  • http://en.wikipedia.org/wiki/Basic_access_authentication

基本身份验证更容易实现,但出于安全原因,我建议使用摘要.如果您的HTTPServer不支持,请尝试更改配置.

Basic auth is much easier to implement, but I'd recommend using digest for security reasons. If your HTTPServer doesn't support that, try to get the configuration changed.

这篇关于如何发出REST GET请求(带有身份验证)并在javascript中解析结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆