I'm asking for some help and explanation as I'm just confused.
I have a jQuery function that, with ajax, is doing a POST call to some server.
var login = $("#email1").val();
var password = $("#password1").val();
var summary = $("#summary").val();
var details = $("#details").val();
var address = $("#address").val();
var str = login + ':' + password;
var credentials = utf8_to_b64(str);
function utf8_to_b64( str ) {
return window.btoa(unescape(encodeURIComponent( str )));
}
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + credentials);
},
crossDomain: true,
//fails with or without next two arguments
username: login,
password:password,
url: "https://blablablasomethingrelevant",
type: "POST",
dataType: "json",
contentType: "application/json",
data: JSON.stringify({"fields":{properdatahere}}),
async: false,
success: function (data) {
alert(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
alert(xhr.responseText);
alert(xhr);
},
});
The error I get is "No 'Access-Control-Allow-Origin' header is present on the requested resource". Of course I googled for it and answer is always that it is "self explaining" blabla error, and the fault in on the server side and I need to do some tricks over there. These answers would make sense if I would fail to perform the same POST call with all other technologies.
When I did exactly the same with java, using jersey client it worked fine, the same using rest client from wiztools.org (had to tick option preemptive)
Client client = Client.create();
client.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(login, password));
WebResource webResource = client.resource("https://blablablasomethingrelevant");
String input = "{"fields":{properdatahere}";
ClientResponse response = webResource.type("application/json").post(ClientResponse.class, input);
So can anybody tell me why it works with java and jersey and not with jquery and ajax? How to make it finally work with ajax?