3

Web アプリケーションに NGINX と sparkjava を使用しています。すべての CORS ヘッダーを適切に有効にしたと確信しています。それでも、「XMLHttpRequest はhttp://localhost:3003/platformAPI/loginを読み込めません。無効な HTTP ステータス コード 404」というエラーが発生します。ブラウザのネットワークタブを調べて、応答ヘッダーと要求ヘッダーも送信されました。それらについては、以下でも説明します。私のアプローチの何が問題なのかを私に知らせる助けがあれば、大歓迎です:)

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
4

2 に答える 2

4

次のスニペットを使用して、Spark-Java で CORS を有効にしてみてください。

        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", "*");
    });
于 2015-11-16T13:12:22.233 に答える
1

この点も考慮する必要があります。サーバーを直接呼び出すため、Postman で動作します。ここには CORS は含まれていません。しかし、javascript や angular などでそれを行うと、CORS が登場します。したがって、要求された URL には http スキームが含まれていません。localhost:portNumber/login をhttp://localhost:portNumber/loginに変更する必要があります

于 2018-11-03T15:23:33.307 に答える