简体   繁体   English

无法使JavaScript到达Rails XML页面上的红宝石

[英]Unable to make a javascript get to a ruby on rails XML page

I have an application running on Ruby on Rails that returns an XML page like this 我有一个在Ruby on Rails上运行的应用程序,该应用程序返回这样的XML页面

<?xml version="1.0" encoding="UTF-8"?>
<list>
  <item title="Professor" href="437">name1</item>
  <item title="Professor" href="1">name2</item>
  <item title="Professor" href="2">name3</item>
  <item title="Professor" href="438">name4</item>
  <start>1</start>
  <end>4</end>
</list>

which I get by accessing 我通过访问获得

http://localhost:3001/people.xml http:// localhost:3001 / people.xml

When, in the browser, I get this page everything is fine but if I use this following code in jquery 当在浏览器中获得此页面时,一切都很好,但是如果我在jquery中使用以下代码

$.get('http://localhost:3001/people.xml', function(data) {
  console.log(data);
});

I get, in firebug, the following error: 我在萤火虫中收到以下错误:

XML Parsing Error: no element found Location: moz-nullprincipal:{ca8a9975-1761-4999-a65a-fe7d0a810762} Line Number 1, Column 1: XML解析错误:找不到元素位置:moz-nullprincipal:{ca8a9975-1761-4999-a65a-fe7d0a810762}行号1,列1:

What can it possibly be ? 可能是什么?

Thanks! 谢谢!

The problem was that the port where I was making the get request (3000) was different from the one who had the data (3001), thus receiving this error. 问题是我发出获取请求(3000)的端口与拥有数据(3001)的端口不同,因此收到此错误。

This happens because one is not able to make ajax get's to a website running in a different domain or port. 发生这种情况是因为无法使Ajax获取到在其他域或端口中运行的网站。

The solution passed by making the request in ajax to my own controller (in port 3000) and, in the controller, making the request to the port 3001 and then returning it back. 该解决方案通过在Ajax中向我自己的控制器(端口3000)发出请求,然后在控制器中向端口3001发出请求,然后将其返回来。

Keep in mind that in ajax you can only make requests to the same domain, in the same port ! 请记住,在阿贾克斯只能发出请求到同一个域,相同的端口

Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM