简体   繁体   中英

Jquery to parse Json “Origin null is not allowed by Access-Control-Allow-Origin”

Where am i going wrong with this

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>title</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$.get("http://api.angel.co/1/tags/1654/startups?callback=aaa",
   function(data) {
     $('body').append( "Name: " + data );
   }, "json");

</script>

    </head>

<body>

</body>
</html>

XMLHttpRequest cannot load http://api.angel.co/1/tags/1654/startups?callback=aaa . Origin null is not allowed by Access-Control-Allow-Origin.

Try use jquery ajax :

$.ajax({
    url:"http://api.angel.co/1/tags/1654/startups?callback=aaa",
    type:'GET',
    dataType:'JSONP',
    success: function(data){
        $('body').append( "Name: " + data );
    }
});

Origin null is not allowed by Access-Control-Allow-Origin

You can't hit another domain ( different from what you are on ) using XMLHttpRequest unless you use JSONP

read more about Same_origin_policy

You must be hosted on some server to run AJAX otherwise it will always say

Origin null is not allowed ...

Try WAMP/LAMP or use Apache tomcat to run your HTML code on localhost. It will make your domain localhost instead of null and will fix it.

PS: Cross domain problem may still persist depending upon the server you are hitting whether it allows you to get/post data from/to it.

The following should work:

$.getJSON("http://api.angel.co/1/tags/1654/startups?callback=?", function(data) {
    $(body).append(data);
});

jQuery will replace the ? with a generated function name that calls the inline function.

Include the attribute, "dataType" with the value "JSONP" in yours ajax call. Refer the below example code:

$.ajax({
    url: "http://api.angel.co/1/tags/1654/startups?callback=aaa",
    dataType:'JSONP',
    success: function(data) {
        $('body').append( "Name: " + data.name);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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