【发布时间】:2016-05-05 05:01:27
【问题描述】:
更新 - 2016-01-30
按照我在 cmets 中的建议:
- 已安装 XAMPP
- 将我的页面放在 Apache 上并运行服务器
- 确保页面和脚本正在执行(简单警报测试)
- 将 xhrFields: withCredentials 设置为 false 以按照唯一答案中的建议发出 CORS 请求并教导 here
我仍然无法通过请求。这是控制台日志:
XMLHttpRequest cannot load http://localhost:8080/RimmaNew/rest/appointments.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 400
------------------初始票--------------- --
我有一个本地运行的 java rest 应用程序,目前接受一个 get 方法并返回一个 json 字符串:
另一种方法是 POST,它应该转换、验证和保存已发送表单中的信息并返回 200 答案。如果在转换、验证或持久性期间出现任何问题 - 将引发异常(例如 BadRequest 或 500)。
这是其余方法(为了简洁起见,我省略了验证、构建方法以及转换器和转换器提供程序类):
@POST
@Produces("application/json")
@Consumes("application/x-www-form-urlencoded")
public Response bookAppointment(@FormParam("date") Date appDate,
@FormParam("time") Time appTime, @FormParam("type") String appType,
@FormParam("clientName") String clientName, @FormParam("email") String clientEmail,
@DefaultValue("") @FormParam("message") String clientMsg) {
//externalize the validation of all fields to concentrate on "positive"
//scenario only
validator(appDate, appTime, appType, clientName, clientEmail);
Appointment appointment = build(appDate, appTime, appType,clientName,
clientEmail, clientMsg);
try {
repository.add(appointment);
return Response.ok(appointment).build();
} catch (Exception e) {
throw new InternalServerErrorException("Something happened in the application "
+ "and this apointment could not get saved. Please contact us "
+ "to inform us of this issue.");
}
}
客户端不在应用程序中(我认为这可能会有用)——它是我计算机上的一个简单的 HTML 文件,其中包含这个 jQuery 脚本:
<script type='text/javascript'>
$(document).ready(function(){
$('form#appointmentForm').submit(function(ev){
ev.preventDefault();
$.ajax({
url: 'localhost:8080/RimmaNew/rest/appointments',
type: 'post',
dataType: 'json',
data: $('form#appointmentForm').serialize(),
contentType: 'application/x-www-form-urlencoded',
beforeSend: function() {
$('#ajaxResponse').html("<img src='245.gif' />");
},
success: function(data) {
$('#ajaxResponse').html("<span style='color:white; background-color: green;' class='glyphicon glyphicon-ok'></span><p>"+JSON.stringify(data)+"</p>")
.fadeIn("slow");
},
error: function(xhr, ajaxOptions, thrownError){
$('#ajaxResponse').html("<span style='color:white; background-color: red;' class='glyphicon glyphicon-exclamation-sign'></span><p>Status: ").append(xhr.status)
.append("</p>").fadeIn("slow");
}
});
});
});
</script>
我想提交一个表单,以便使用 @FormParam 注释属性访问其参数。在我发送的每个请求中,我都没有收到任何抛出的错误,并且状态始终为 0。您知道我在哪里出错或缺少什么吗?
【问题讨论】:
-
你说的 是什么意思...客户端不在应用程序中 - 它是我计算机上的一个简单的 HTML 文件,有这个 jQuery 脚本",ajax 失败是由于如果您从
file://协议运行该脚本,则到同源。 -
它是一个没有部署到任何应用服务器的html文件。这个文件只是驻留在 /home/myusername/subdir1/subdir2/RimmaNew/ 你是说我不能像这样使用 jQuery 吗?有解决办法吗?
-
您可以使用 jQuery,但如果此 .html 文件不是从具有
http协议的实际网络服务器提供的,则您违反了同源策略,并且无法执行 ajax ,因为协议、端口和域必须匹配。 -
谢谢。老实说,我从来不知道同源政策——现在刚刚在 wiki 上阅读过它。这篇文章似乎有一个解决方法:stackoverflow.com/questions/3076414/…
-
有一些变通方法,其中大多数不适用于
file://协议,在你的情况下,你应该启动一个 WAMP 服务器或类似的东西,你很好。跨度>
标签: javascript java jquery json rest