【发布时间】:2018-02-09 18:24:54
【问题描述】:
我的标题“Access-control-allow-origin”有问题,我使用下一个代码发出请求:
<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.mensaje);
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("error");
}
});
}
</script>
以及它是由 java 客户端做出的响应:
@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();
}
当尝试从标头“Access-Control-Allow-Origin”指示的地址访问时,在浏览器控制台中出现此错误:
XMLHttpRequest 无法加载 http://sdpeapp00024.pe.intranet:9080/ccws/rest/ops/pcnct020。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://i6.sura.pe' 因此不允许访问。
我不知道我还能做什么。我尝试添加其他标头,例如来自 java 代码的响应中的 cmets,但我总是得到相同的错误。
非常感谢您的帮助。
更新:
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{
}
}
我用java做了一个客户端并且工作正常。我真的不知道为什么使用 Ajax 不起作用,但这表明 Web 服务工作正常并且问题出在客户端。
您好。
PS。在代码中我没有放置 url,因为我不允许发布超过两个 url,但它们与错误消息中显示的相同。
【问题讨论】:
-
看来您需要为端点启用 CORS 检查此spring.io/guides/gs/rest-service-cors
-
谢谢,但我没有使用 Spring Framework。
标签: javascript java ajax cors cross-domain