Javid
Javid

Reputation: 343

Success function is not getting invoked in ajax GET method

In this code I am facing a problem in the success function. I tried a lot but it is going to the error function. The #action-button calls the JavaScript Method.

<button id="action-button">Click me to load info!</button>
<div id="info"></div>
<small>
    Demo created by <a href="https://twitter.com">Twitter</a>
</small>
$('#action-button').click(function() {
    $.ajax({
        url: 'sampleurl',
        data: { format: 'json' },
        error: function() {
            $('#info').html('<p>An error has occurred</p>');
        },
        dataType: 'jsonp',
        success: function(data) {
            $('#info').html('<p>Successd</p>');
        },
        type: 'GET'
    });
});

Upvotes: 0

Views: 109

Answers (3)

Phylogenesis
Phylogenesis

Reputation: 7890

As I have mentioned in the comments above, the AJAX query is being blocked because of XSS protection.

Running your provided jsFiddle in IE (and placing a breakpoint in the error() callback) gives the error:

Access is denied

And running it in Firefox gives the following error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://0daf70fe0a6244fe... This can be fixed by moving the resource to the same domain or enabling CORS.

You will have to add an appropriate Access-Control-Allow-Origin header to the API call at http://sampleURL.

Upvotes: 1

hamed
hamed

Reputation: 8043

If any problem happen during ajax call, such as, wrong url or error on server, success function will not be fired. And if every thing is ok, HTTP status will be 200 OK and therefore success event will be fired. Also, you have dataType: 'jsonp' in your options that does not match with post data. This can be one possible problem. You can check the error in the Firebug and post it for better help.

Upvotes: 0

Ofer Vugman
Ofer Vugman

Reputation: 36

If you're sending json to the server try adding conetentType: application/json

Also are you expecting the response to be of jsonp content type ? mismatch in this can also be the issue.

Upvotes: 2

Related Questions