简体   繁体   中英

AJAX - Cross-domain don't work

I was reading many things about that the json is great replacement for XMLHttpRequests. I tried it and it don't works:

$.ajax({
crossDomain: true,
    url: settingsURL,
type: "POST",
dataType: 'JSONP',
parseAsHtml: true, cli: 'help',
    success: function(data) {
        data=$(data).find('div#TestDivContent');
        $('#TestDivContent').append(data);
    },
error: function() {
        $('#TestDivContent').append("<p>Can't Connect</p>");
    }
});

and im getting...

Uncaught SyntaxError: Unexpected token < 

Please Check the code below that is working like a charm in Cross Domain (). if You Have control of both the Domains ie, Domain1.com & Domain2.com

//Ajax Script in Domain1.com
//No Conflict is the code snippet from my sample code You can delete it if not required no issues
<script type="text/javascript">jq1102 = jQuery.noConflict( true );</script>
<script type="text/javascript" >
    function jsonp(n){
        //GET Response is Here
        alert(n);
    }

    jq1102(function(){
        jq1102.ajax({
            type: "GET",
            dataType: "jsonp",
            url: 'http://domain2.com/ClientSiteApi/',
            crossDomain: true,
            complete: function(data){
                //Any Action You Like to Trigger After Complete 
            },
            error: function(jqXHR, textStatus, ex) {
                //Nothing to Change Here
            }
        });
    })
</script>

Response from Domain2.com

 echo 'jsonp("hello")'; //You Can place JSON string in replace of the Hello String

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