Pradyumna
Pradyumna

Reputation: 167

Uncaught syntax error: Unexpected token < , ajax call

<script>
    (function(){
        var searchURL = 'http://en.wiktionary.org/wiki/search';
        $.ajax({
                type: "GET",
                url: searchURL,
                dataType: "jsonp",
                cache: false,
                async:false,
                success: function(responseData, textStatus, XMLHttpRequest){
                        iframe(responseData);
                    }
            });
    })();
    </script>

I added this script to my html file and it is show the following errors, copy pasting the function in console is also showing the same errors.

Uncaught SyntaxError: Unexpected token < 
Resource interpreted as Script but transferred with MIME type text/html

could anyone help me resolve this issue, I am using Chrome brower.

Upvotes: 0

Views: 4549

Answers (1)

gen_Eric
gen_Eric

Reputation: 227220

You can't request arbitrary pages via AJAX, and jsonp doesn't magically make that work. You need to use the Wiktionary API.

The URL is http://en.wiktionary.org/w/api.php.

$.ajax({
    url: 'http://en.wiktionary.org/w/api.php',
    dataType: 'jsonp',  // will automatically add "?callback=jqueryXXX"
    cache: true,  // the API complains about the extra parameter
    data: {  // the parameters to add to the request
        format: 'json',
        action: 'query',
        titles: 'test'
    },
    success: function(data){
        console.log(data);
    }
});

Upvotes: 3

Related Questions