正如标题所说,我正在尝试使用jQuery AJAX调用Web URL http://host:port/...或http://localhost:8080/...本地HTML文件来访问(POST)c:\home.html.我无法让它发挥作用.
我做谷歌,也看到了几个问题,但我无法让它工作.我需要一些帮助.这是我到目前为止所尝试的内容.
response.setHeader("Access-Control-Allow-Origin", "*");
Run Code Online (Sandbox Code Playgroud)
这三种浏览器都不起作用 - IE,FF或Chrome.请求永远不会到达服务器.以下是我看到的一些错误.
这是我的代码.我将不胜感激任何帮助.我正在使用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)
一个例子是一个完美的指针.
我正在尝试解决 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) 我正在尝试为我的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)
如果有人能告诉我为什么过滤器似乎没有应用于响应,那就太好了.
我试图通过使用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) cors ×3
rest ×2
scala ×2
ajax ×1
angularjs ×1
cross-domain ×1
javascript ×1
jquery ×1
preflight ×1