【发布时间】:2026-01-26 16:50:01
【问题描述】:
我正在使用 Java Restfull Webservice,它在 postgres SQL 中转储数据。 Java WebService 功能完美运行。功能如下所示
@Path("/db")
public class DBOperaions {
@POST
@Path("/insert")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public String InsertDBData(@PathParam("hname") String hname,
@PathParam("hdates") String hdates,
@PathParam("hremark") String hremark,
@PathParam("isDelete") boolean isDelete,
@PathParam("created_date") String created_date,
@PathParam("updated_date") String updated_date) {
//Data Dumping Code
}
}
现在我正在尝试从 Jquery Ajax 访问此功能,如下所示
var obj = { hname: $("#txtRuleName").val(), hdates: $("#txtRuleDates").val(), hremark: $("#txtRuleRemark").val(), isDelete: false, created_date: 'CURRENT_TIMESTAMP', updated_date: 'CURRENT_TIMESTAMP' };
$.ajax({
type: "POST",
contentType: "application/json",
dataType: "json",
xhrFields: {
withCredentials: true
},
url: "http://localhost:8015/PostGresTestDB/rest/db/insert",
data: JSON.stringify(obj),
success: function (data, textStatus, jqXHR) {
alert('Data Inserted successfully...');
},
error: function (jqXHR, textStatus, errorThrown) {
alert('generateReportFromMR:Error in processing!');
//alert(jqXHR);
}
});
Webservice 运行在带有http://localhost:8015/PostGresTestDB/rest/db/insert 的tomcat 服务器上 和运行在http://localhost:42229/index.html上的网页
我收到以下错误
XMLHttpRequest 无法加载 http://localhost:8015/PostGresTestDB/rest/db/insert。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'http://localhost:49927'。
我尝试了 CORS,但没有成功。
谁能告诉我我到底想为此做什么?
【问题讨论】:
标签: java jquery web-services postgresql