使用jquery ajax不返回xml数据

时间:2021-11-28 07:37:46

I am trying to get the page popularity of a site

我试图获得一个网站的页面流行度

<POPULARITY URL="google.com/" SOURCE="panel" TEXT="1"/>

using alexa api. If I post

使用alexa api。如果我发布

http://data.alexa.com/data?cli=10&dat=snbamz&url=http://www.google.com 

into the browser i get a xml response but using ajax I get nothing returned

进入浏览器我得到一个xml响应,但使用ajax我没有得到任何回报

$.ajax({    type: "GET",
            url: "http://data.alexa.com/data?cli=10&dat=snbamz&url=http://www.google.com",
            dataType: "xml",
            cache: false,
            success:function(data){

            alert(data); 

            }

        });

What am i doing wrong?

我究竟做错了什么?

2 个解决方案

#1


0  

I would structure it like this:

我会像这样构造它:

$.get(ajax_url, data, function(response) {
        alert(response);
  });

Also, I think you want be doing a get for the data (rather than $.post and then specifying get later on).

另外,我认为你想要获取数据(而不是$ .post然后再指定get)。

#2


0  

I imagine it's because you're trying to load an xml file which is not from your domain. Most browsers will block this by default because it breaks cross-domain javascript rules.

我想这是因为你正在尝试加载一个不是来自你域的xml文件。大多数浏览器会默认阻止此操作,因为它会破坏跨域JavaScript规则。

If you look in the console in your developer tools (F12 in most browsers) you'll see an error similar to this:

如果您在开发人员工具中查看控制台(在大多数浏览器中为F12),您将看到与此类似的错误:

XMLHttpRequest cannot load http://data.alexa.com/data?cli=10&dat=snbamz&url=http://www.google.com&_=1337464540283. Origin null is not allowed by Access-Control-Allow-Origin.

XMLHttpRequest无法加载http://data.alexa.com/data?cli=10&dat=snbamz&url=http://www.google.com&_=1337464540283。 Access-Control-Allow-Origin不允许使用null。

The easiest way around this (presuming you are running PHP) is to create a small php file which wraps the xml file on your own server and load it from there.

最简单的方法(假设您正在运行PHP)是创建一个小的php文件,它将xml文件包装在您自己的服务器上并从那里加载它。

see this question for an example:

看到这个问题的例子:

Ajax: Load XML from different domain?

Ajax:从不同的域加载XML?

#1


0  

I would structure it like this:

我会像这样构造它:

$.get(ajax_url, data, function(response) {
        alert(response);
  });

Also, I think you want be doing a get for the data (rather than $.post and then specifying get later on).

另外,我认为你想要获取数据(而不是$ .post然后再指定get)。

#2


0  

I imagine it's because you're trying to load an xml file which is not from your domain. Most browsers will block this by default because it breaks cross-domain javascript rules.

我想这是因为你正在尝试加载一个不是来自你域的xml文件。大多数浏览器会默认阻止此操作,因为它会破坏跨域JavaScript规则。

If you look in the console in your developer tools (F12 in most browsers) you'll see an error similar to this:

如果您在开发人员工具中查看控制台(在大多数浏览器中为F12),您将看到与此类似的错误:

XMLHttpRequest cannot load http://data.alexa.com/data?cli=10&dat=snbamz&url=http://www.google.com&_=1337464540283. Origin null is not allowed by Access-Control-Allow-Origin.

XMLHttpRequest无法加载http://data.alexa.com/data?cli=10&dat=snbamz&url=http://www.google.com&_=1337464540283。 Access-Control-Allow-Origin不允许使用null。

The easiest way around this (presuming you are running PHP) is to create a small php file which wraps the xml file on your own server and load it from there.

最简单的方法(假设您正在运行PHP)是创建一个小的php文件,它将xml文件包装在您自己的服务器上并从那里加载它。

see this question for an example:

看到这个问题的例子:

Ajax: Load XML from different domain?

Ajax:从不同的域加载XML?