1
 $.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?

2
  • Is your app running on any server? or checking with html file? Commented May 21, 2015 at 5:56
  • my application is running on my local server and the api is running on different server Commented May 21, 2015 at 6:05

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

i'm using post request and jsonp works with get request and even tried this but still not working : var headers = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT', 'Content-Type': 'application/json', 'Accept': 'application/json' }; and in ajax : headers: headers,
'Access-Control-Allow-Origin' should be put in the server response and not request.
ok, but how to overcome this issue? is there anything wrong in the format of json i'm sending?
no issue with you request, are you able to change server response ?
server is responding with a bad request and there is a DHC plugin in chrome when i paste the data there server response is success.
|
0

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', '*'); } 

2 Comments

Access-Control-Allow-Origin is for response and not request
Exactly what @kwan245 said. Your addition of the header won't do anything. It's the server-side that controls what can access it.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.