Paranoid42
Paranoid42

Reputation: 116

problems with cross domain ajax

I'm simple trying to get the message from http://bootcamp.jit.su/welcome/marian

Here is the code i'm using

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <meta charset="utf-8" />
        <title>Globant :: Welcome to the HTML5 bootcamp</title>
    </head>
    <body>

    </body>
        <script type="text/javascript">
        $(document).ready(function(){
            var URL = "http://bootcamp.jit.su/Welcome/marian?callback=?";
            var request = jQuery.noConflict().ajax({
                    url: URL,
                    type: "GET",
                    dataType: 'jsonp',
                    processData: false,
                    success: function(data) {alert('hola') ;}
            });
        });

    </script>

</html>

It seems that it should work fine, but i keep getting an error message in the console : "unexpected ':' character"

Upvotes: 1

Views: 353

Answers (1)

Onur Topal
Onur Topal

Reputation: 3061

The thing is JSONP is not an actual AJAX request. It is a workaround to trick browser and it is working by inserting a script tag to header. Think, this your JSONP supported URL

domain.com/jsonp.aspx?callback=processJSONP

the processJSONP is a function you already have in your page and the requests return a script similar to this

processJSONP( {
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
});

it calls your function with the data you need and you do what ever you want to do. of course the data don't have to be a JSON object or you can get more callback like

processJSONP("data1=11");
processJSONP("data2=22");
processJSONP("data3=33");

So you cannot get the data you need if the URL doesn t support.

To achieve what you want here you have to use a server side script (e.g. ASP.Net, PHP etc) and Request the content by a server side object (WebRequest, HttpRequest) and when you get it just you can use it as a JSONP or standard AJAX request as it is in your own domain right now.

Hope this clears everything.

Upvotes: 1

Related Questions