jQuery get函数不返回数据 [英] jQuery get Function Not Returning Data

查看:226
本文介绍了jQuery get函数不返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jQuery get函数来呈现我页面上的div中的HTML页面. get函数的结果似乎是成功的,因为textStatus的值(在下面的代码块中)为成功".但是,数据值始终为空.

$(document).ready(function() {
  $.get('http://www.google.com', function(data, textStatus){
    $('#RSSContent').html(textStatus + ' ' + data);
  });
});

知道我可能在做什么错吗?

注意:最终代码将查询GeoRSS提要,我计划在RSSContent div中将其转换和呈现为HTML.我只是使用 http://www.google.com 进行测试.

解决方案

您无法向这样的另一个域发出AJAX请求.您遇到的问题称为相同来源政策.出于安全原因,不允许javascript发出这样的请求(在某些情况下,例如 JSONP ).

想象一下,如果我的广告脚本在您的网页中(通过document.write或任何 XSS 方法)能够复制您的名称和密码字段,并通过AJAX将其提交到后台的另一个域中.

I'm trying to use the jQuery get function to render the results of an HTML page in a div on my page. The result of the get function appears to be successful because the value of textStatus (in the following code block) is "success". The value of data, however, is always empty.

$(document).ready(function() {
  $.get('http://www.google.com', function(data, textStatus){
    $('#RSSContent').html(textStatus + ' ' + data);
  });
});

Any idea what I might be doing wrong?

Note: The final code will query a GeoRSS feed, which I plan on transforming and rendering as HTML in the RSSContent div. I simply used http://www.google.com for testing purposes.

解决方案

You can't make an AJAX request to another domain like this. What you're running into is called the same origin policy. For security reasons, javascript isn't allowed to make a request like this (except in certain cases like JSONP).

Imagine if my ad script in your page (via document.write or any XSS method) was able to copy your name and password fields and submit it to another domain in the background via AJAX...bad idea :)

这篇关于jQuery get函数不返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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