XMLHttpRequest cannot load http://xxx.xxx. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 500.
我正在尝试使用 ajax 发送 xml soap,但出现了该错误。我尝试了很多选项,但似乎没有任何效果,这是代码:
var soapMessage =
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://xxx.xxx/">'+
'<soapenv:Header/>'+
'<soapenv:Body>'+
'<wsdl:test1>'+
'<PUI>12345</PUI>'+
'</wsdl:test1>'+
' </soapenv:Body>'+
'</soapenv:Envelope>';
$.ajax({
url: 'http://xxx.xxx',
type: 'POST',
dataType: 'xml',
data: soapMessage,
crossDomain: true,
processData: false,
contentType: 'text/xml; charset=\"utf-8\"',
headers: {
SOAPAction: "http://xxx.xxx"
},
success: function (msg, data) {
alert(msg);
},
error: function (msg, data) {
alert("Error");
}
});
我做错了什么?我发送了一个 POST Action ,但它把它读作 OPTION。 如何解决这个问题?
我使用 Boomerang Rest 和 Soap Client 来测试这个服务,它给我正确的响应。当我如上所述使用我自己的程序时,它给我 XMLHttpRequest cannot load http://xxxxx “错误。我正在使用 apache tomcat 6.0 并为代码使用 Java web 应用程序
最佳答案
您正在执行跨域请求,因此您向其发出请求的服务器必须发送一个 Access-Control-Allow-Origin 响应 header 以表明它允许跨域请求.
参见 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS了解详情。
For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. For example, XMLHttpRequest and Fetch follow the same-origin policy. So, a web application using XMLHttpRequest or Fetch could only make HTTP requests to its own domain.
OPTIONS 请求发生的原因是,当您发送带有 Content-Type header 的跨源请求时,该 header 的值不是 application/x-www-form-urlencoded、multipart/form-data 或 text/plain,您的浏览器首先执行 CORS preflight检查。
您的请求发送Content-Type: text/xml; charset="utf-8",这样会导致预检。
就解决方法而言,如果您向其发送请求的服务器不是您可以控制和配置的服务器,您可以使用开放式反向代理,例如 https://cors-anywhere.herokuapp.com/ .
它的工作方式是,您不是将请求直接发送到 http://xxx.xxx,而是发送到 https://cors-anywhere.herokuapp.com/http://xxx.xxx。并且代理您的请求并使用 Access-Control-Allow-Origin 和其他预期的 CORS header 进行响应。
当然,您需要意识到,如果您的请求包含任何凭据,您就会将这些凭据公开给 cors-anywhere.herokuapp.com 的维护者。
关于ajax - 对 OPTIONS 的 POST 更改 + 对预检请求的响应未通过访问控制检查 : No 'Access-Control-Allow-Origin' header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41874845/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以