0

I have following codes. I used it to get some data from another domain address. In my local network. If anyone can tell me What I did mistake here or give me resolves.

<html>
  <title>Jquery Json</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
  <script type="text/javascript">
    jQuery(document).ready(function($){
      $.ajax({ // ajax call starts
        type:"GET",
        async: false,
        cache: false,
        contentType: "application/json; charset=utf-8",
        crossDomain: true,
        //url: "http://10.1.128.107/JsonDohaBackToTemplate/getProjectIDByMAC.svc/media/?projectid=1",
        url: "http://fantasy.premierleague.com/web/api/elements/415/?_=1357337284504", 
        success: function(data) {
          $("body").append(JSON.stringify(data));
        }, 
        error: function(jqXHR, textStatus, errorThrown) {
          alert(jqXHR.status);
        },
      dataType: "jsonp"
    });
  });
  </script>
  </head>

  <body>
  </body>
</html>
Alejandro Galera
  • 3,445
  • 3
  • 24
  • 42
yeshansachithak
  • 832
  • 2
  • 18
  • 34
  • 2
    Another domain? You are not allowed, your probably get a security error right? – putvande Aug 14 '13 at 09:02
  • @putvande I've just use url: "http://fantasy.premierleague.com/web/api/elements/415/?_=1357337284504" for test. But when I used url: "http://10.1.128.107/JsonDohaBackToTemplate/getProjectIDByMAC.svc/media/?projectid=1", my domain also same error. It's is like an alert box displaying 200 – yeshansachithak Aug 14 '13 at 09:04
  • It is a crossdomain policy, you can't connect to another domain via JavaScript. – putvande Aug 14 '13 at 09:05
  • How I get xml data from that address call 10.1.128.107...-> I need to get xml data from it and display it. My IP address is 10.1.128.103 – yeshansachithak Aug 14 '13 at 09:08

2 Answers2

1

Javascript is not allowed to load resources cross-domain. You need to look into Cross Origin Resource Sharing (cors). Check out these questions:

Make cross-domain ajax JSONP request with jQuery

jQuery AJAX cross domain

And of course wikipedia:

http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

Community
  • 1
  • 1
gimg1
  • 1,121
  • 10
  • 24
0

Remove the following from your call: async, cache, crossDomain, contentType, and error. These all not needed to make crossdomain call, but may be the reason why it's not working for you, because it looks like you have a usual call. E.g. error handler is never called for crossdomain

Here is what you need as minimum:

$.ajax({
                        url: '',
                        success: function (data, textStatus, jqXHR) {     
                        },
                        dataType: 'jsonp'
                    });

UPDATE: Since you are getting XML, not jsonp, then you have only 2 choices:

1) Add following headers to your response, if you have access to server:

name="Access-Control-Allow-Origin" value="*"
 name="Access-Control-Allow-Headers" value="Content-Type"

2) Use server side proxy

Vitaly
  • 2,064
  • 19
  • 23
  • Hi! I'm a new for this. Can you explain me one by one – yeshansachithak Aug 14 '13 at 09:35
  • Please see update. In order to crossdomain work, you need also server support. If you are receiving XML, jsonp will not work because jsonp requires server to return jsonp data type – Vitaly Aug 14 '13 at 10:09