开发者

Problems with Ajax get Request

开发者 https://www.devze.com 2023-02-04 08:01 出处:网络
I have problems with this function: 开发者_高级运维<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">

I have problems with this function:

开发者_高级运维
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<title>Untitled Document</title>
<script type="text/javascript">
jQuery(document).ready(function(){
$.get("http://www.boersenpoint.de/pages/charts/shareSupplier.php?request=topFlop",     {     indexName: "DAX" } ,function(data){   
  $(data).find('share').each(function(){
 alert('');
 });

 });
 });
 </script>
 </head>

<body>
</body>
</html> 

Firebug can not find any errors. By theory 4 alerts should appear, but the don't. Why?


The problem is the same-origin policy. This means that you cannot do AJAX requests to a domain unless the page you are on is also on that domain.

The easiest way to get around it would be to set up a script on your server that proxied the requests -- you make a request to your server, the server makes the request to the other server and feeds the response back to you.

If the remote server supports it, you could also use JSONP, but I doubt that is possible as the page is an XML document.


$(function(){
  $.get("http://www.boersenpoint.de/pages/charts/shareSupplier.php?request=topFlop", {indexName: "DAX"}, function(data) {
    $(data).find('share').each(function(){
      alert('');
    });
  });
});

There are a couple problems with your code...

  1. You are making a remote request via ajax which is explicitly prohibited by the same origin policy
  2. EVEN IF you weren't violating that policy, that doesn't guarantee you'd see an alert - what if data is empty? If so, .find('share').each would not execute ==> no alert.
  3. Some browsers (mostly really old ones) actually ignore the alert(); statement if it doesn't contain text (I don't know about the empty string as you have though, just something to keep in mind).

To summarize, your specific problem in this case is the same-origin policy violation. 2. and 3. are for your benefit in future development.

0

精彩评论

暂无评论...
验证码 换一张
取 消