在没有 xml 文件扩展名的页面中使用 HTTPRequest 获取 xml
我正在尝试使用 jquerys ajax 函数的 HTTPRequest 从页面获取 xml。该函数没有返回任何内容。我的理论是,为什么这不起作用是因为我点击的页面的文件扩展名是“.ns”而不是“.xml”,这是我点击的页面的完整页面源。
<?xml version="1.0" encoding="UTF-8"?>
<logged_in_reps>
</logged_in_reps>
如何从 HTTPRequest 获取此 XML?
一些可能有帮助的附加信息:当我保存网页时,它保存为“command.ns.xml”(该网址只有 .ns 而不是 .xml),当我在与我自己的目录相同的目录中点击此页面时具有请求的文件(带有 .xml 扩展名),它工作正常。
此外,发出请求的文件所在的域是“www.csun.edu”,我正在访问的页面位于“remotesupport.csun.edu”。这会是一个问题吗?
这是拨打电话的代码(不幸的是,我无法提供我所点击的网址的用户名和密码)
$.ajax({
url: 'https://remotesupport.csun.edu/api/command.ns?username=user&password=pass&action=get_logged_in_reps',
type: 'GET',
datatype: 'xml',
success: function(xml) {
alert(xml);
}
});
I am trying to use an HTTPRequest using jquerys ajax function to get the xml from a page. The function is not returning anything. My theory of why this is not working is because the page I am hitting has the file extension ".ns" rather than ".xml" This is the FULL page source of the page I am hitting.
<?xml version="1.0" encoding="UTF-8"?>
<logged_in_reps>
</logged_in_reps>
How can I get this XML from an HTTPRequest?
Some additional information that may help: When I save the webpage it saves as "command.ns.xml" (which the url only has the .ns and not the .xml) and when I hit this page in the same directory as my own file (with the .xml extension) that has the request, it works fine.
Also the domain that the file making the request is on is "www.csun.edu" and the page i'm hitting is on "remotesupport.csun.edu". Could this be a problem?
Here is the code to make the call (unfortunately I cannot provide the username and password with the url I am hitting)
$.ajax({
url: 'https://remotesupport.csun.edu/api/command.ns?username=user&password=pass&action=get_logged_in_reps',
type: 'GET',
datatype: 'xml',
success: function(xml) {
alert(xml);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了解决同源策略问题,以及您想要从中获取 XML 的服务器不支持 JSONP,您可以使用 YQL。
这是一个示例
然后您可以使用它作为 JSONP 请求的 URL:
http://query.yahooapis.com/v1/public/yql?q=select * from xml where url="http://the-xml-url.com"
所以对于你来说你有问题会做这样的事情:
To get round the Same Origin Policy issue, and the fact that the server you want to get the XML from doesn't support JSONP, you can use YQL.
Here's an example
You would then use this as the URL for your JSONP request:
http://query.yahooapis.com/v1/public/yql?q=select * from xml where url="http://the-xml-url.com"
So for you problem you would do something like this:
有两个主要问题。
首先,由于同源策略,您被拒绝访问该 XML 文件。
其次,
dataType
参数必须用作确切的字符串。There are two main problems.
First, you are denied access to that XML file because of Same Origin Policy.
Second, the
dataType
argument must be used as that exact string.是的,问题是名为同源策略的安全限制。
您可以使用 JSONP 技术规避它(请参阅 此处 解释的 JQuery 支持),但这需要您包装生成的 XML 输出。
另一种解决方案是通过源服务器(即您的情况下的
www.csun.edu
)代理您的 Ajax 请求。Yes the problem is the security restriction called Same Origin Policy.
You can circumvent it using the JSONP technique (see JQuery support explained here), but that requires you to wrap the generated XML output.
Another solution is to proxy your Ajax request through the origin server (that is,
www.csun.edu
in your case).