jQuery的Ajax的POST到Rails 3.2.2不通过访问控制 - 允许 - 原产地允许 [英] jQuery Ajax POST to Rails 3.2.2 not allowed by Access-Control-Allow-Origin
问题描述
我有一个简单的阿贾克斯后,以在Heroku上运行的导轨支架项目:
I have a simple .ajax post to a rails scaffold project running on Heroku:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"> </script>
</head>
<body>
<script>
$(document).ready(function(){
$.ajax({
type: 'POST', url: "http://herokuserver/images.json",
data: { image: { name: "johngalt1" } }
});
});
</script>
</body>
事实证明使用JavaScript控制台中铬我得到:
As it turns out using the javascript console in Chrome I get:
XMLHttpRequest cannot load http://herokuServer/images.json. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
然而,当我去在Heroku上的Rails应用了新的记录已创建名为=>johngalt1。从我读过的一切我认为跨域问题的JavaScript将有pvented正在创建的记录$ P $,并要求我使用JSON-P或CORS?或者通过JavaScript做的JavaScript跨域问题仅prevent从服务器接收数据?
HOWEVER, when I go to the rails app on heroku a new record has been created with the name => "johngalt1". From everything I've read I thought the cross-domain issue with javascript would have prevented the record being created and required me to use json-p or CORS? Or does the javascript cross-domain issue only prevent receiving data from a server via javascript?
推荐答案
它仅prevents从接收数据的浏览器。所以,如果你不需要从这个帖子的回应,你应该确定。
It only prevents the browser from receiving data. So if you don't need a response from this post, you should be ok.
编辑 - 当我本地开发,我在我的Mac创建Apache2的服务器重写规则来解决这个限制。它会创建一个反向代理的招数浏览器到允许跨域请求。
edit - when I'm developing locally, I create a rewrite rule for the Apache2 server on my mac to get around this limitation. it creates a reverse proxy that tricks the browser into allowing cross-domain requests.
这篇关于jQuery的Ajax的POST到Rails 3.2.2不通过访问控制 - 允许 - 原产地允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!