AJAX POST/GET 请求抛出空错误,但 Glassfish 服务器似乎响应正确

发布于 2024-12-27 22:40:52 字数 1340 浏览 1 评论 0原文

我使用 JAX-RS 在 Glassfish 服务器上设置了一些非常简单的 Web 服务。最初一切似乎都工作正常 - 我会通过浏览器看到预期的响应(例如返回的 HTML)。这是代码:

@GET
@Produces("text/HTML")
public String getHtml() {
    return "<html lang=\"en\"><body><h1>GET RESPONSE</body></h1></html>";
}

@POST
@Produces("text/HTML")
@Consumes("text/plain")
public String postText(String content)
{        
    return "<html lang=\"en\"><body><h1>POST RESPONSE: " + content + "</body></h1></html>";
}

正如您所看到的,它非常基本。

当我尝试用 Javascript 对这些 Web 服务进行 AJAX 调用时,问题就开始了。 POST 和 GET 调用始终失败,没有有用的错误信息。我在 Firefox 的 Firebug 插件中检查了响应结果,响应代码显示 200 ok,尽管 Firebug 在 HTML 面板中没有显示任何内容,这可能表明存在问题。这是 Javascript:

$.ajax({
    type: "POST",
    url: "http://localhost:8080/TestEngine/API/",
    data: "string",
    dataType: "html",
    success: function(data, textStatus, jqXHR) {
        alert("success");
    },
    error: function(jqXHR, textStatus, errorThrown) {                             
        alert("error:" + errorThrown );
    },
    contentType: "text/plain"
});

我尝试了数据、dataType、contentType 等的各种修改和排除,但它们最终总是抛出错误回调。我还尝试修改 GET 和 POST 函数的返回值,包括尝试使用 JAX-RS Responsebuilder(尽管我不熟悉它的用法,所以可能做得不正确)。我仍然以错误回调结束,错误参数中没有明显有用的信息(大多数内容只是返回为未定义或空)。

在这一点上,我真的很感激一些关于我做错了什么的建议。谢谢!

I've setup some very simple web services on my Glassfish server using JAX-RS. Initially everything appeared to be working correctly - I'd see the expected responses through the browser (e.g. the returned HTML). Here's the code:

@GET
@Produces("text/HTML")
public String getHtml() {
    return "<html lang=\"en\"><body><h1>GET RESPONSE</body></h1></html>";
}

@POST
@Produces("text/HTML")
@Consumes("text/plain")
public String postText(String content)
{        
    return "<html lang=\"en\"><body><h1>POST RESPONSE: " + content + "</body></h1></html>";
}

As you can see, it's really basic.

The problem started though when I tried to make AJAX calls in Javascript to these web services. Both POST and GET calls fail consistently, with no useful error information. I check the response results in Firefox's Firebug plugin though and the response codes show 200 ok, though Firebug shows nothing in the HTML panel which may indicate a problem. Here's the Javascript:

$.ajax({
    type: "POST",
    url: "http://localhost:8080/TestEngine/API/",
    data: "string",
    dataType: "html",
    success: function(data, textStatus, jqXHR) {
        alert("success");
    },
    error: function(jqXHR, textStatus, errorThrown) {                             
        alert("error:" + errorThrown );
    },
    contentType: "text/plain"
});

I've tried various modification and exclusions of data, dataType, contentType, etc. but they always end up throwing the error callback. I've also tried modifying the return value of the GET and POST functions, including trying to use the JAX-RS Responsebuilder (though I'm unfamiliar with it's usage, so may have done so incorrectly). I still end up in the error callback, with no apparent useful info in the error arguments (most stuff just returns as undefined or empty).

At this point I'd really appreciate some advice as to what I'm doing wrong. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末染殇 2025-01-03 22:40:52

我解决了这个问题——我必须了解 CORS。最终的问题是标头中的原点为空,因为我在本地工作。将以下代码添加到我的 POST 响应函数中解决了该问题(尽管我需要在生产服务器上提供更强大的功能):

ResponseBuilder builder = Response.ok("<html lang=\"en\"><body><h1>POST RESPONSE</h1></body></html>");
builder.header("Access-Control-Allow-Origin", "*");
return builder.build();

I solved the issue - I had to learn about CORS. Ultimately the issue was that the origin in the header was null because I was working locally. Adding the following code to my POST response function resolved the issue (though I'll need something more robust on the production server):

ResponseBuilder builder = Response.ok("<html lang=\"en\"><body><h1>POST RESPONSE</h1></body></html>");
builder.header("Access-Control-Allow-Origin", "*");
return builder.build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文