程序go 2016-10-16 14:39 采纳率: 100%
浏览 50

Prolog中的CORS不起作用

I have a problem on cors in prolog. I think it not working.

Edit #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).

However I use ajax post to prolog server it said

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.

Ajax that I use to post.

   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);
     } 
   })

How can I solve this problem?

-Edited Now I fix some of my code but it still not work.

  • 写回答

2条回答 默认 最新

  • 三生石@ 2016-10-16 21:38
    关注

    Quoting from the documentation of library(http/http_cors):

    Because CORS is a security risc (see references), it is disabled by default. It is enabled through the setting http:cors. The value of this setting is a list of domains that are allowed to access the service. Because * is used as a wildcard match, the value [*] allows access from anywhere.

    (Emphasis mine.)

    Thus, in your server, you probably must include the setting:

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

    By default, nothing is written by cors_enable/0.

    Next time, please construct a minimal example that others can actually try out.

    EDIT: Your edited code has elementary problems that have nothing to do with CORS. Please reduce this to a minimal example. You can see what is wrong by:

    1. Starting the server with ?- server(4050). (for example)
    2. Visiting, in your web browser, http://localhost:4050.

    You will see error messages that indicate problems.

    Here is a small skeleton for handle/2 that you can extend as necessary:

    handle(Request) :-
            cors_enable,
            reply_json(test{x: 1, y: 2}).
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号