rohit singh
rohit singh

Reputation: 590

No 'Access-Control-Allow-Origin' header is present error in AJAX Request

  $.ajax({
            url: 'http://test.aegi.com/rest/social/update/@ViewBag.orgId?access_token=6fWV564u7rATh8=',
            type: 'POST',
            contentType: 'application/json',
            data: [{
                "message": "It's party time",
                "messageType": "NEW",
                "gmtTimeDate": "2014-12-21 23:59:59",
                "soAccts": [{
                    "accountId": "74470431",
                    "soAccountType": "FBPAGE"
                }]
            }],

            success: function (data) {
                console.log(data);
            },
            error: function (error) {
                console.log(error);
            }

        });

When i'm trying to send this request AJAX i'm getting an error : No 'Access-Control-Allow-Origin' header is present.
But on dhc chrome extension when i put the same above data in body section and send the request then i get a success response. Am i doing anything wrong here?

Upvotes: 1

Views: 2287

Answers (3)

yogesh hotchandani
yogesh hotchandani

Reputation: 11

you can use json dataType:"jsonp" in your ajax request

Upvotes: 0

kwangsa
kwangsa

Reputation: 1711

Modern Browser prohibit cross origin request hence they needs 'Access-Control-Allow-Origin' response header to be present , if not then you receive that error.

While chrome extension like postman circumvent this restriction.

If you have an access to server then you should add 'Access-Control-Allow-Origin' into response header or using jsonp.

Check CORS for more info.

Upvotes: 1

Hardy
Hardy

Reputation: 542

Try by adding header :

headers: { 'Access-Control-Allow-Origin', '*' }
* in above line will allow access to all domains.

headers: { 'Access-Control-Allow-Origin', 'http://www.example.com' }
For allowing access to specific domain only.

Edited answer:

beforeSend: function(xhr) {
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
}

Upvotes: 0

Related Questions