jquery - Prolog 中的 CORS 不起作用

标签 jquery ajax prolog cors swi-prolog

我在 prolog 中遇到了 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 post 来 prolog 服务器,它说

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。

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

如何解决这个问题?

-已编辑 现在我修复了一些代码,但它仍然无法工作。

最佳答案

引自documentation 库(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.

(强调我的。)

因此,在您的服务器中,您可能必须包含以下设置:

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

默认情况下,cors_enable/0 不写入任何内容。

下次,请构建一个其他人可以实际尝试的最小示例。

编辑:您编辑的代码存在与 CORS无关的基本问题。请将其简化为最小示例。您可以通过以下方式查看问题所在:

  1. 使用 ?- server(4050). 启动服务器(例如)
  2. 在网络浏览器中访问 http://localhost:4050

您将看到指示问题的错误消息。

这是 handle/2 的一个小框架,您可以根据需要进行扩展:

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

关于jquery - Prolog 中的 CORS 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071513/

相关文章:

prolog - 给定现金金额和货币列表生成找零

javascript - Owl Carousel - 仅用一个点旋转木马幻灯片滑动多个旋转木马

javascript - AJAX 请求从每个页面都会命中服务器,但我找不到原始 AJAX 调用的来源

prolog - 寻求 (\=)/2 的纯 Prolog 实现

javascript - Ajax 文件上传 (HTML5)

javascript - 实现 Microsoft 的 Project Oxford - Emotion API 和文件上传

prolog - 替换 Prolog 中列表的元素

javascript - FullCalendar Jquery,选择当天内

jQuery .在悬停时附加此内容

javascript - Flask:不要重定向到 url()