相关疑难解决方法(0)

jQuery ajax POST从本地文件访问跨域无法正常工作

正如标题所说,我正在尝试使用jQuery AJAX调用Web URL http://host:port/...http://localhost:8080/...本地HTML文件来访问(POST)c:\home.html.我无法让它发挥作用.

我做谷歌,也看到了几个问题,但我无法让它工作.我需要一些帮助.这是我到目前为止所尝试的内容.

  1. dataType:jsonp
  2. crossDomain:true
  3. 在我的回复中设置标题:
response.setHeader("Access-Control-Allow-Origin", "*");
Run Code Online (Sandbox Code Playgroud)

这三种浏览器都不起作用 - IE,FF或Chrome.请求永远不会到达服务器.以下是我看到的一些错误.

  1. 如果不使用jsonp,则不传输(IE).
  2. 在FF中加载内容(NS_ERROR_DOCUMENT_NOT_CACHED)时出现NS_BINDING_ABORTED /错误

这是我的代码.我将不胜感激任何帮助.我正在使用jquery-1.8.2.min.js.

var http_host =  "http://localhost:8080";

function su (pc, p) {
    var suUrl = http_host + "/ps/api/v2/authorize.json";

    $.ajax({
        type: 'POST',
        url: suUrl,
        data: {
            phone_cell: pc,
            password: p,
        },
        dataType: "json",
        crossDomain: true,
        success: osu,
        error: oe
    });
    return false;
}

function osu (d) {
    console.log(d);
}

function oe(xhr, ts, et)     {
    alert("ServerError: " + et);
}
Run Code Online (Sandbox Code Playgroud)

一个例子是一个完美的指针.

ajax jquery cross-domain cors

5
推荐指数
1
解决办法
9495
查看次数

在 Scala Play Framework v2.6.x 中获取 cors 错误

我正在尝试解决 Scala/Play 2.6.x 中一个简单的“hello world”样式 REST API 的 CORS 错误,并且我已经尝试了此时我能想到的所有方法。据我所知,在互联网上找不到好的解决方案或示例,所以即使这应该是一个简单的解决方案,那么任何有好的解决方案的人都会通过完整发布来真正帮助我。我只是想从localhost:3000(使用 axios 的反应应用程序)向localhost:9000我的 Scala/Play 框架所在的位置发送一个 post 请求。

错误

我在客户端遇到的错误如下:

XMLHttpRequest cannot load http://localhost:9000/saveTest.
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:3000' is therefore not allowed
access. The response had HTTP status code 403.
Run Code Online (Sandbox Code Playgroud)

我在服务器端遇到的错误是

success] Compiled in 1s

--- (RELOAD) ---

[info] p.a.h.EnabledFilters - Enabled Filters
(see <https://www.playframework.com/documentation/latest/Filters>):

    play.filters.csrf.CSRFFilter
    play.filters.headers.SecurityHeadersFilter
    play.filters.hosts.AllowedHostsFilter
    play.filters.cors.CORSFilter

[info] play.api.Play - …
Run Code Online (Sandbox Code Playgroud)

rest scala cors playframework preflight

5
推荐指数
1
解决办法
4061
查看次数

Play Framework CORS Headers

我正在尝试为我的play框架应用程序设置CORS Headers.具体来说,我收到了这个错误

cannot load http://127.0.0.1:9000/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)

我想我可以按照以下说明轻松处理:https: //www.playframework.com/documentation/2.5.x/CorsFilter

但是,这样做之后.什么也没有变.

curl -I localhost:9000/
HTTP/1.1 200 OK
Content-Length: 4540
Content-Type: text/html; charset=utf-8
Date: Mon, 11 Jul 2016 20:03:33 GMT
Run Code Online (Sandbox Code Playgroud)

我的担心是:

play.http.filters = "global.Filters"

play.filters.cors {
  allowedOrigins = ["http://www.example.com", "*"]
  allowedHttpMethods = ["GET", "POST"]
  allowedHttpHeaders = ["Accept"]
}
Run Code Online (Sandbox Code Playgroud)

和我的Filters.scala文件是:

package global

import javax.inject.Inject
import play.api.http.DefaultHttpFilters
import play.filters.cors.CORSFilter

class Filters @Inject() (corsFilter: CORSFilter)
  extends DefaultHttpFilters(corsFilter)
Run Code Online (Sandbox Code Playgroud)

如果有人能告诉我为什么过滤器似乎没有应用于响应,那就太好了.

scala cors playframework

4
推荐指数
2
解决办法
8852
查看次数

如何在play-framework 2.5.x中实现跨源资源共享(CORS)

我试图通过使用Angularjs-1应用程序从localhost命中Restful URL获取json数据.

我收到了这个错误

http://localhost:9000/mlm/user/all Failed to load resource: 
the server responded with a status of 404 (Not Found)

index.html:1 XMLHttpRequest cannot load http://localhost:9000/mlm/user/all. 

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:63342' is therefore not allowed access. 

The response had HTTP status code 404.
Run Code Online (Sandbox Code Playgroud)

我正在使用play-framework 2.5.4(java).

编辑1:为app.conf添加了CORS设置

    play.filters {
    cors {
    # Filter paths by a whitelist of path prefixes
    pathPrefixes …
Run Code Online (Sandbox Code Playgroud)

javascript rest playframework angularjs playframework-2.3

4
推荐指数
1
解决办法
2863
查看次数