我建立與JAX-RS web服務,我已經啓用了兩個服務器端和AJAX請求CORS後,我仍然得到一個錯誤說:AJAX請求阻塞甚至使CORS
跨來源請求阻止:同源策略不允許在http://www.bookstore.com/orderservice/order處讀取遠程資源。這可以通過將資源移動到相同的域或啓用CORS來解決。有什麼想法嗎?謝謝。
服務器端代碼:
<jaxrs:ser <bean id="cors-filter" class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter"/>ver name="OrderService" address="http://www.bookstore.com">
<jaxrs:serviceBeans>
<ref bean="orderServiceRest" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jacksonProvider" />
<ref bean="cors-filter" />
</jaxrs:providers>
<jaxrs:schemaLocations>
< <jaxrs:schemaLocation>com.bookengine.ws.service.representation.OrderRequest</jaxrs:schemaLocation>
<jaxrs:schemaLocation>com.bookengine.ws.service.representation.OrderRepresentation</jaxrs:schemaLocation>
</jaxrs:schemaLocations>
</jaxrs:server>
<bean id="orderServiceRest" class="com.bookengine.ws.service.OrderResource" />
AJAX Request
$.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Access-Control- Allow-Origin': "http://www.bookstore.com/orderservice"
},
type: 'POST',
crossDomain: true,
contentType: 'application/json',
dataType: 'json',
data: '{"orderId":null,"bookId":"X1","status":null,"customer":{"name":"John Smith","address":"312 N State Street, Chicago IL 60611","phone":"312-345-9876","custId":"1234"},"paymentinfo":null}',
url: 'http://bookstore/orderservice/order',
error: function(xhr, status, error) {
// Display a generic error for now.
alert("AJAX Error!");
},
success: function (data) {
var output = "<ul>";
output += "<li>" + "Order ID: " + data.orderID
+ "<br>Book ID: " + data.bookID
+ "<br>Status: " + data.status
+ "</li>";
output += "</ul>";
alert("Hello from sucess");
document.getElementById("buydata").innerHTML = output;
}
});