使用 JS 将帖子发送到不同的域
我希望在某个文本输入字段更改后使用 javascript 发送一个发布请求。
这是我当前的代码:
<input name="message" onchange="$.ajax({type: \"POST\", url: \"http://example.com/example.php\", data: \"message=\" + document.getElementsByName(\"message\")[0].value});" />
现在,它可以在常规连接上工作,但不能在安全连接(SSL)上工作。 我的意思是,该页面是安全的,但请求被发送到非安全页面。
有解决办法吗?
I'd like a post request to be sent once a certain text input field is changed, using javascript.
So here is my current code:
<input name="message" onchange="$.ajax({type: \"POST\", url: \"http://example.com/example.php\", data: \"message=\" + document.getElementsByName(\"message\")[0].value});" />
Now, it's working on a regular connection, but it's not working on a secured connection (SSL).
I mean, the page is secured, but the request is sent to a non secured page.
Is there a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将表单的 target 属性设置为指向隐藏的 iframe。
您将无法阅读响应,但可以提出请求。
如果您想读取响应,您将需要通过您自己的服务器代理请求。
Set the target attribute of the form to point to a hidden iframe.
You won't be able to read the response, but you can make the request.
If you want to read the response, you will need to proxy the request through your own server.
正如 David 指出的,由于同源策略的(相当合理的)限制,不可能对另一个域上的服务执行异步 POST。 JSON-P 之所以有效,是因为您可以将标签插入到 DOM 中,并且它们可以指向任何地方。
您可以使用 JSONP 进行跨域 AJAX w/ GET: JSONP CrossDomain
它还介绍了如何使用 jQuery 执行 JSONP
同源策略,防止从一个源加载的文档或脚本从另一个源获取或设置文档的属性。如果两个页面的协议、端口和主机相同,则认为两个页面具有相同的来源。 http://rj3.net/mdc/sop
确保在 ajax url 中指定 ssl,当页面的其余部分也使用 ssl,例如 https
您无法将其从 https 发送到 http 站点。任何 https 资产(html 或其他)只能由同一域/ssl 证书上的内容访问。因此,您将无法执行您想要执行的操作(https 到 http)。因此,由于您的页面是在 https 上提供的,因此由于政策原因,目标 http 站点无法访问它
As David points out, t is not possible to do an asynchronous POST to a service on another domain, due to the (quite sensible) limitation of the same origin policy. JSON-P only works because you're allowed to insert tags into the DOM, and they can point anywhere.
YOu can do cross-domain AJAX w/ GET using JSONP: JSONP CrossDomain
It also covers how to do JSONP w/ jQuery
same origin policy prevents a document or script loaded from one origin from getting or setting properties of a document from another origin. Two pages are considered to have the same origin if the protocol, port, and host are the same for both pages. http://rj3.net/mdc/sop
make sure you specify ssl in the ajax url, when the rest of your page uses ssl too e.g https
You can't send it from a https to a http site. any https asset (html or otherwise) can only be accessed by something on the same domain / ssl certificate. so, you won't be able to do what you are trying to do (https to http). so since your page is served on https the targeted http site can not access it due to the policy