NGINX和Spark Java之间的跨源通信 [英] Cross origin communication between NGINX and spark Java

查看:221
本文介绍了NGINX和Spark Java之间的跨源通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将NGINX和sparkjava用于我的Web应用程序.我确定我已正确启用所有CORS标头.不过,我仍然得到"XMLHttpRequest无法加载 http://localhost:3003/platformAPI/login 无效的HTTP状态代码404"错误.下面分别是我的extjs和spark java的客户端和服务器方法.我已经检查了浏览器的网络"标签,以获取发送的响应和请求标头.它们也在下面提到.非常感谢您提供任何有关我的方法有什么问题的帮助:)

I am using NGINX and sparkjava for my web application. I am sure I have enabled all CORS headers properly. Still, I am getting "XMLHttpRequest cannot load http://localhost:3003/platformAPI/login. Invalid HTTP status code 404" error.Below mentioned are my client and server methods from extjs and spark java respectively. I have inspected the network tab of my browser to get the response and request headers sent as well. They are also mentioned below. Any help to let me know as to what's wrong with my approach is highly appreciated :)

Nginx的客户端方法:

function(button, event, options){
           var form = Ext.getCmp("LoginformId").getForm();
             if (form.isValid()) {
                    var userJson = JSON.stringify(form.getFieldValues());
                    form.submit({
                        url: 'http://localhost:3003/platformAPI/login',
                        //headers : {'Content-Type':undefined,},
                        //dataType: 'jsonp',
                        success: function(form, action) {
                      // Ext.Msg.alert('Success', action.result.msg);
                       var sessionID=action.result.sessionID;
                       var clientName=action.result.clientName;
                       sessionStorage.setItem('sessionID',sessionID);
                       sessionStorage.setItem('clientName',clientName);                                             
                       window.location="http://localhost:3000/";
                        },
                        failure: function(form, action) {
                            Ext.Msg.alert('Failed', action.result.msg);
                        }
                    });

            }
        }

服务器方法:

过滤器以启用CORS标头(在主体中调用)

private static void enableCORS(final String origin, final String methods, final String headers) {
        before(new Filter() {
            @Override
            public void handle(Request request, Response response) {                
                response.header("Access-Control-Allow-Origin",request.headers("origin"));
                response.header("Access-Control-Allow-Headers", "Origin, x-requested-with, content-type, Accept");
                response.header("Access-Control-Request-Method", "GET,PUT,POST,DELETE,OPTIONS");
               );
            }
        });

    }

登录方式:

post("platformAPI/login", "undefined",
                (request, response) -> {
                    System.out.print("inside login");
                    JSONObject object1 = new JSONObject();
                    response.body(object1.put("success", true).toString());
                    return response;
                });

请求和响应标头:

Remote Address:127.0.0.1:3003
Request URL:http://localhost:3003/platformAPI/login
Request Method:OPTIONS
Status Code:404 Not Found


Response Headers
view source
Access-Control-Allow-Headers:Origin, x-requested-with, content-type, Accept
Access-Control-Allow-Origin:http://localhost:3000
Access-Control-Request-Method:GET,PUT,POST,DELETE,OPTIONS
Cache-Control:must-revalidate,no-cache,no-store
Content-Length:295
Content-Type:text/html; charset=ISO-8859-1
Server:Jetty(9.0.2.v20130417)


Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:x-requested-with, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:3003
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36

推荐答案

尝试使用以下代码段在Spark-Java中启用CORS:

Try enable CORS in Spark-Java with the following snippet:

        options("/*",
            (request, response) -> {

                String accessControlRequestHeaders = request
                        .headers("Access-Control-Request-Headers");
                if (accessControlRequestHeaders != null) {
                    response.header("Access-Control-Allow-Headers",
                            accessControlRequestHeaders);
                }

                String accessControlRequestMethod = request
                        .headers("Access-Control-Request-Method");
                if (accessControlRequestMethod != null) {
                    response.header("Access-Control-Allow-Methods",
                            accessControlRequestMethod);
                }

                return "OK";
            });

    before((request, response) -> {
        response.header("Access-Control-Allow-Origin", "*");
    });

这篇关于NGINX和Spark Java之间的跨源通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆