</div>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/21783079/ajax-in-chrome-sending-options-instead-of-get-post-put-delete" dir="ltr">AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE?</a>
<span class="question-originals-answer-count">
(11 answers)
</span>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/35553500/xmlhttprequest-cannot-load-xxx-no-access-control-allow-origin-header" dir="ltr">XMLHttpRequest cannot load XXX No 'Access-Control-Allow-Origin' header</a>
<span class="question-originals-answer-count">
(8 answers)
</span>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it" dir="ltr">Why is an OPTIONS request sent and can I disable it?</a>
<span class="question-originals-answer-count">
(14 answers)
</span>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2017-05-05 09:10:11Z" class="relativetime">2 years ago</span>.</div>
</div>
</aside>
Continue to receive this as a return for a $.ajax post.
"401 (API requests require HTTP Basic Auth or OAuth.) Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."
Returns 200 OK with postman or hurl.it.
I have written the following below continue to see the above error. Should I be adding headers under url instead of in the beforeSend, or have I omitted an additional unknown parameter?
This is an ajax POST for http://api.myemma.com/ and the credentials and account ID being used are valid.
var data = {
"fields": {
"first_name": "fname",
"last_name": "lname",
"postal_code": "00000"
},
"email": "test@testing.com",
},
username = "username",
password = "password",
accountid = "0000000";
$.ajax({
type: "POST",
url: "https://api.e2ma.net/"+accountid+"/members/add",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(data),
xhrFields: {
withCredentials: true
},
beforeSend:function(xhr){
console.log("beforesend");
xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
},
success:function(data){
alert("success");
},
error:function(error){
console.log(error);
}
});
</div>