Mein Problem ist, wenn ich eine Anfrage an einen Jaxrs API-Server aus dem Browser sendet es aus Preflight Anfrage sagenSo senden Sie Anwendung/JSON jQuery AJAX-Anfrage in Jaxrs ohne 'Access-Control-Allow-Origin' fehlenden Fehler
Ursprungsübergreifende Anforderung blockiert: Die Richtlinie "Gleiche Herkunft" verbietet das Lesen der Remote-Ressource unter https://localhost:9091/api/employee/add. (Grund: CORS-Header 'Access-Control-Allow-Origin' fehlt).
Ich verstehe nicht, warum ich die Preflight-Anfrage Erfolg nicht machen konnte. Meine Postbittenanfrage war erfolgreich. Aber wenn die Anfrage vom Browser gesendet wird, fällt sie.
Ajax Anfrage:
$.ajax({
type: "POST",
url: "https://localhost:9091/api/employee/add",
cache: false,
data:JSON.stringify(employeeJSONObj),
dataType: "json",
contentType: 'application/json',
success: function(response){
});
Json Nutzlast:
{
"employeeName":"Name3",
"nRIC":"NRIC3",
"dateOfBirth":"2017-01-02",
"address":"Address",
"salaryMode":"Monthly",
"bankAccount":"Bank Account",
"appointmentAs":"Security Supervisor (SS)",
"nationality":"Malaysian",
"maritalStatus":"Married",
"sex":"Female",
"nextOfKin":"Next Of kin",
"mobilePhone":"Mobile Phone",
"profilePicture":null,
"document":null
}
Jersy jaxrs API-Server Integration:
@POST
@Consumes({ MediaType.APPLICATION_JSON})
@Produces({ MediaType.APPLICATION_JSON})
@Path("add")
public Response addEmployee(Employee employee) {
EmployeeImpl employeeImpl =
(EmployeeImpl) context.getBean("employeeImpl");
if (employeeImpl.validateEmployeeDetails(employee)) {
employeeImpl.addEmployee(employee);
employee.setSuccessMessage("Employee added successfully");
}else{
employee.setErrorMessage("employee detail insertion failed due to invalid" +
" inputs, please recheck inputs and retry");
return Response.ok().status(Response.Status.BAD_REQUEST)
.entity(employee)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST, OPTIONS")
.header("Access-Control-Allow-Headers","Content-Type")
.build();
}
return Response.ok()
.entity(employee)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST, OPTIONS")
.header("Access-Control-Allow-Headers","Content-Type")
.build();
}
Maven Abhängigkeiten:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2</artifactId>
<version>1.8</version>
</dependency>
Anfrage Header:
Host: localhost:9091
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: http://localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Nicht mit Trikot vertraut aber scheint dem Rücken Ende ist das Hinzufügen nicht CORS-Header auf 'OPTIONS' Anfragen. Postman ist nicht CORS beschränkt und sendet sie nicht als Preflight – charlietfl
Wie sollte ich CORS-Header auf OPTIONS Anfrage hinzufügen –
Wie gesagt, ich bin nicht vertraut mit Jersey, sondern sollte leicht zu erforschen sein. Lesen Sie, wie CORS auch funktioniert – charlietfl