I am encountering an issue with the header "Access-control-allow-origin" when making a request using the following code:
<script type='text/javascript'>
function save() {
$.ajax(
{
type: 'POST',
url: "...",
contentType: 'application/json',
data: '{"cuspp":"228061JGLIR5", "userWeb":"46689"}',
success: function (data) {
console.log("It Works");
console.log (data);
if (data.codigo==0){
console.log(data.mensaje);
}else{
console.log(data.message);
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("error");
}
});
}
</script>
The response is generated by a Java client as follows:
@POST
@Path("/pcnct020")
@ApiOperation(value = "Save events.", notes = "PCNCT020", responseClass =
"data.Answer")
public Response saveEvents(
@ApiParam(value="Structure of Event", required = false) Evento event) {
Answer<Result> answer = Validator.validate(event);
if (answer.esOK()) {
int size = event.textDetail.length();
int count = size / 60;
String comment = event.textDetail;
int secuence = 0;
for (int j = 0; j <= count; j++) {
evento.secuence = secuence;
String newString;
if (j == 0) {
if (size < 60) {
newString = comment.substring(j * 60);
} else {
newString = comment.substring(j * 60,
(j * 60) + 60);
}
} else if (j == count) {
newString = comment.substring(j * 60);
if (newString.equals("")) {
break;
}
} else {
newString = comment.substring(j * 60,
(j * 60) + 60);
if (newString.equals("")) {
break;
}
}
event.textDetail = newString;
answer.setAnswer(event.saveEvent());
secuence = Integer.parseInt(answer.ans.status);
}
}
return Response
.status(200)
.header("Access-Control-Allow-Origin", "...")
//.header("Access-Control-Allow-Credentials", "true")
//.header("Access-Control-Allow-Headers", "Origin")
//.header("Access-Control-Allow-Methods", "GET, POST, DELETE,
PUT, PATCH, HEAD, OPTIONS")
//.header("Conten-Type","application/application/json")
.entity(answer)
.build();
}
Upon trying to access from the specified address, I encounter an error in the browser console related to the "Access-Control-Allow-Origin" header:
XMLHttpRequest cannot load . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access.
I have attempted adding additional headers following the comments in the Java response code with no success.
Your assistance would be greatly appreciated.
UPDATE:
public void getService(){
try {
String urlWS = "Web Service Url";
String url = urlWS;
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", "application/json");
StringEntity params =new StringEntity("
{\"cuspp\":\"228061JGLIR0\", \"usuarioWeb\":\"46683\");
httpPost.setEntity(params);
CloseableHttpResponse response =
httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
System.out.println(response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200){
BufferedReader brResponse = new BufferedReader(new
InputStreamReader(response.getEntity().getContent()));
String responseText = "";
String output = "";
while ((output = brResponse.readLine()) != null) {
responseText += output;
}
System.out.println(responseText);
}
} catch (Exception excepcion) {
System.out.println(excepcion.toString());
}
finally{
}
}
A Java client was created which functions correctly. While unsure why Ajax is not functioning properly, this demonstrates that the web service operates correctly and the issue lies within the client.
Warm regards.
In the interest of privacy, URLs have been omitted from the code.