2016-10-16 3 views
2

プロローグのコルスに問題があります。私はそれが動作しないと思う。プロローグのCORSが機能しない

編集#1

:- module(server,[]). 

:- use_module(library(http/thread_httpd)). 
:- use_module(library(http/http_dispatch)). 
:- use_module(library(http/http_cors)). 
:- use_module(library(http/http_json)). 
:- use_module(library(http/json_convert)). 
:- use_module(library(option)). 
:- use_module(library(settings)). 
:- http_handler(root(.),handle,[]). 

:- set_setting(http:cors, [*]). 
server(Port) :- 
    http_server(http_dispatch,[port(Port)]). 

:- json_object 
     poke(pokemon:text, move:text). 
handle(Request) :- 
    format(user_output,"Request is: ~p~n",[Request]), 
    format(user_output,"Request2 is: ~p~n",[]), 
    cors_enable, 
    http_read_json_dict(Request, DictIn,[json_object(term)]), 
    format(user_output,"I'm here~n",[]), 
    term_string(Pokemon,DictIn.pokemon), 
    findall(poke(P,M),beat(P,M,Pokemon),L), 
    prolog_to_json(L,J), 
    format(user_output,"Pokemons are: ~p~n",[J]), 
    DictOut=J, 
    reply_json(DictOut). 

beat(P,M,E) :- 
    pokerule:beat(P,M,E). 

私はプロローグサーバにAJAXポストを使用ししかし、それは私が投稿する際に使用

XMLHttpRequest cannot load http://localhost:9999/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:1000' is therefore not allowed access. The response had HTTP status code 500. 

アヤックスと述べました。

let enemyName = this.item.text 
    let data = {"pokemon":enemyName} 
    $.ajax({ 
    url  : END_POINT, 
    method  : 'post', 
    contentType: 'application/json', 
    dataType : 'json', 
    data  : JSON.stringify(data), 
    success : function (res) { 
     console.log(res); 
    }, 
    error :function (res) { 
     console.log(res); 
    } 
    }) 

どうすればこの問題を解決できますか?

-edd 私はコードを修正しましたが、まだ動作しません。

答えて

4

library(http/http_cors)documentationから引用:CORSセキュリティRISC(参考文献を参照)であるので、それはデフォルトでは無効になって

http:corsという設定で有効になっています。この設定の値は、サービスにアクセスできるドメインの一覧です。 *はワイルドカードの一致として使用されるため、値[*]はどこからでもアクセスできます。デフォルトでは

 
:- set_setting(http:cors, [*]). 

、がcors_enable/0によって書かれ何も:

(重点鉱山)が

このように、サーバーには、おそらく設定を含める必要があります。

次回は、他の人が実際にを試してみるという最小の例を構築してください。

EDIT:編集したコード  CORSでを行うにはは何もありません基本的な問題があります。これを最小値の例に減らしてください。 Webブラウザ、http://localhost:4050で、?- server(4050).

  • (例えば)訪問してサーバーを起動する

    1. :あなたは間違っていることでものを見ることができます。

    問題を示すエラーメッセージが表示されます。

     
    handle(Request) :- 
         cors_enable, 
         reply_json(test{x: 1, y: 2}). 
    
  • +0

    私は自分のコードにその行を追加します。

    は、ここでは、必要に応じて拡張することができhandle/2ための小さなスケルトンです。しかしそれでも動作しません。 – ReiiYuki

    +0

    私はこのコードを完全に表示するようにこの投稿を編集します。 – ReiiYuki

    +0

    私は自分の答えを更新しました。関係のないいくつかの問題があなたの実際の質問を覆していることに注意してください。次回は、できるだけ短くしてください。 – mat