gpt4 book ai didi

JAVA JSON Restful Web 服务请求的资源上不存在 'Access-Control-Allow-Origin' header

转载 作者:行者123 更新时间:2023-12-01 21:36:30 24 4
gpt4 key购买 nike

我有一个 JAVA RESTful Web 服务,它将返回 JSON 字符串,并且它是用 Java 编写的。我的问题是当我使用以下 URL 向该网络服务发送请求时

http://localhost:8080/WebServiceXYZ/Users/insert

它给我以下错误消息

XMLHttpRequest cannot load http://localhost:8080/WebServiceXYZ/Users/insert/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 500.

这是我的代码

package com.lb.jersey;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.json.JSONException;
import org.json.JSONObject;

@Path("/Users")
public class RegistrationService
{
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/insert")
public String InsertCredentials (String json) throws JSONException
{
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);

String phone = null;
JSONObject returnJson = new JSONObject();
try
{
JSONObject obj = new JSONObject(json);
JSONObject result1 = obj.getJSONObject("Credentials");
phone = result1.getString("phone");
DBConnection conn = new DBConnection();

int checkUserID = conn.GetUserIDByPhone(phone);
if(checkUserID <= 0)
{
DBConnection.InsertorUpdateUsers(phone, currentTime);
}

int userID = conn.GetUserIDByPhone(phone);
int otp = (int) Math.round(Math.random()*1000);

DBConnection.InsertorUpdateCredentials(userID, otp, currentTime);

JSONObject createObj = new JSONObject();
createObj.put("phone", phone);
createObj.put("otp", otp);
createObj.put("reqDateTime", currentTime);
returnJson.put("Credentials", createObj);

System.out.println(returnJson);
}
catch (Exception e)
{
}
return returnJson.toString();
}
}

我的 web.xml 代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>WebServiceXYZ</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.lb.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

我已经阅读了很多文章,但没有任何进展,所以请告诉我,我该如何处理这个问题?

最佳答案

这是假设您使用 jetty 作为服务器。希望它有帮助...

将以下代码添加到您的 web.xml

<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>allowedMethods</param-name>
<param-value>GET,POST,DELETE,PUT,HEAD</param-value>
</init-param>
<init-param>
<param-name>allowedHeaders</param-name>
<param-value>origin, content-type, accept</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

以及 pom.xml 中的以下依赖项

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>8.0.0.M0</version>
</dependency>

配置tomcat的链接是link ...这是另一个 link2相应修改:)

关于JAVA JSON Restful Web 服务请求的资源上不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com