- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
尝试在服务器上启用 CORS 的情况下访问不同域中的 Web 服务。当我使用 Postman 测试 Web 服务时,一切正常,我确实收到了以下响应 header :
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, DELETE, PUT, OPTIONS
Access-Control-Allow-Origin:*
当我的网络应用程序 (Angular 2) 尝试向其发送 POST 时,我得到:
XMLHttpRequest cannot load http://example.com/mywebservice. No 'Access-Control-Allow-Origin' header is present on the requested resource.
我不确定为什么 Postman 似乎收到了正确的变量设置,但我的浏览器仍然阻止我使用它。
根据 Chrome 调试器,预检响应似乎工作正常:
Request Method:OPTIONS
Status Code:200 OK
当我尝试访问网络服务的 GET 部分时,一切正常:
getJob(id: number): Observable<Job> {
let myParams = new URLSearchParams();
myParams.append('id', id + "");
let options = new RequestOptions({ headers: this.getHeaders(), params: myParams});
return this.http
.get(this.jobsUrl, options)
.map(res => res.json().data as Job)
.catch(this.handleError);
}
失败的是网络服务的 POST 部分:
createJob(job: Job): Observable<any> {
let options = new RequestOptions({ headers: this.getHeaders()});
return this.http
.post(this.jobsUrl, job, options)
.map(res => res.json().jobID)
.catch(this.handleError);
}
这里是 getHeaders():
private getHeaders() {
let headers = new Headers();
headers.append('Content-Type','application/json');
return headers;
}
最佳答案
以下错误消息可能令人困惑:
XMLHttpRequest cannot load http://example.com/mywebservice. No 'Access-Control-Allow-Origin' header is present on the requested resource.
当服务器上的 CORS 设置不正确时,可能会出现此消息,但如果服务器本身以非 2XX 状态代码(即 500)响应,也可能会出现此消息。我想,因为这是一个错误,所以服务器觉得不需要填充适当的支持 CORS 的响应 header 。当浏览器看不到这些 header 时,它会返回类似 CORS 的错误。
如果您好奇的话,我的 Web 服务失败的原因是因为我试图将“application/json”数据提交到它不支持的 Web 服务。一旦我将其切换为使用“application/x-www-form-urlencoded”并稍微修改了 Angular 语法,它就可以正常工作。
关于angular - 获取 "No ' Access-Control-Allow-Origin' header is present on the requested resource"错误,即使 CORS 设置正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45527935/
let appDelegate = UIKit.UIApplication.shared.delegate! if let tabBarController = appDelegate
我的演示文稿终于成功了。我的第一个屏幕有一个主要的 activity,第二个屏幕有一个 Presentation。我的问题是,我无法更改演示 View 中的内容。 为什么我不能在第二个屏幕上显示演示文
这个问题在这里已经有了答案: ios, getting a pointer to a controller from a view (5 个答案) 关闭 4 年前。 我有一个 uiview 文件,我
我有一个表示小部件的 View 类和一个随附的演示者类。我还有一个 View 类,用于拥有小部件的窗口,以及窗口 View 的随附演示者。窗口操作小部件,所以我需要窗口展示器与小部件展示器进行通信。可
关闭相机后。它给了我这个警告:在演示过程中尝试继续演示! - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picke
由于这个错误,我无法切换到另一个 View Controller 。我想在成功扫描二维码后切换到另一个 View Controller 。 2015-01-27 17:59:16.093 *[5416
有时它会发生 - 从代码的不同地方 - 我们想要做 UIViewController.presentViewController(a, b, c),有时我们已经在呈现了,在这种情况下我们得到: 警告:
这是我的第一个 iOS 应用程序。 所以我有一个 UIVIewController 和一个 UITableView,其中我按顺序集成了一个 UISearchBar 和一个 UISearchContro
我的模式允许一个特定的元素是可选的,但是当我稍后在文件中遇到一个不同的也是可选的元素时,我需要前一个元素存在。我如何通过 XSD 确保这一点? 例子: true 应该是有效的,
我正在使用 xcode 4.5.2 并为相机按钮创建了自己的 OverlayViewController。 现在,当我的委托(delegate) Controller - TakePhotoViewC
如何在 MVP 中创建 View ? Presenter 是否总是创建它们(除了 subview 的 View 之外)?或者它是一个单独的第三方组件或应用程序或创建它们的东西? 我们还要补充一点,我可
当我调用viewController.presentViewController时,如果已经存在另一个 Controller ,则会出现以下警告。 Warning: Attempt to presen
我有一个关于 React Native 的模块,并在这个问题的标题中抛出错误。这是模块的代码,我不知道 swift 或 Objective-C,所以我需要你像“swift for idiots”一样解
我在 didSelectRowAtIndexPath 中选择一个 segue 时收到上面的警告。这发生在 iPad 上。 iPhone 给出了不同的警告,我会看看是否可以解决这个问题。 我在方法中确实
我的应用程序中有一个包含两个场景的 StoryBoard - 它使用自动转场(在 IB 中创建)。当在第一个 UIViewController 中单击按钮时,第二个 UIViewController
如何在成功验证登录后转到我的第二页? 我已经从登录页面 View Controller (不是登录按钮)拉出一个 segue 到下一页,并将该 segue 命名为“nextPage”。 (如果我从登录
我正在构建一个应用程序,最近发现了由传统转场引起的巨大内存泄漏。因此我了解了 unwind segue。如果我简单地使用,一切都很好: @IBAction func prepareForUnw
我不知道如何将按钮添加到 link_to_add_fields 中 在我制作的 Rails 应用程序中。 我尝试在各处添加 class: "btn btn-mini btn-info",但我一直收到
大家好,感谢阅读。我正在制作一个应用程序,您可以在其中拍照并从相机胶卷中检索照片并将其显示在 View 中。问题是,当我尝试使用调出相机胶卷的按钮时,出现如上标题中的错误“演示正在进行中”。 如果我注
我有一个模型,其中有两个字段在技术上可以为空。字段名称是 :is_activated 和 :activated_at。 :activated_at 仅在 :is_activated 设置为 true
我是一名优秀的程序员,十分优秀!