jquery通过A$.post发送跨域数据
我正在尝试通过 $.post() 将数据发送到 cakephp (mvc) 网站。下面是代码
$('#testReq').click(function () {
console.log('Button Works');
$.post('http://play.anthonylgordon.com/usersessions/store/', { data: 'test7' }, function (data) {
//data contains the json object retrieved.
console.log(data.status);
}, "json");
})
下面是 cakephp 数据,用于检索数据并存储它。如果你懂蛋糕,那就太好了,但如果不懂也没关系。我真的很想弄清楚我是否正确发送了数据,
<?php
class UsersessionsController extends AppController {
var $name = 'Usersessions';
var $helpers = array('Html', 'Form','Ajax');
var $components = array('RequestHandler');
function store()
{
Configure::write('debug', 0);
$this->autoRender = false;
echo 'hello';
if ($this->params['url']['data'])
{
$this->data['Usersession']['data'] = $this->params['url']['data'];
$this->Usersession->Save($this->data);
echo 'Success';
}
}
}
?>
正如你所看到的,我在进行任何评估之前输入了“hello”。我应该能够在我的控制台中看到这一点,但我没有。我用 get 尝试了这个方法,并且确实看到了响应“hello”。这让我得出的结论是,您无法通过 $.post
跨域发送数据。唯一有效的方法是 getJSON() 除非有人能证明我错了。
I am trying to send data over to a cakephp (mvc) website, via $.post(). below is the code
$('#testReq').click(function () {
console.log('Button Works');
$.post('http://play.anthonylgordon.com/usersessions/store/', { data: 'test7' }, function (data) {
//data contains the json object retrieved.
console.log(data.status);
}, "json");
})
Below is the cakephp data that retrieves the data and stores it. If you know cake, great but if not it's fine. I am really trying to figure out if i am sending the data correctly
<?php
class UsersessionsController extends AppController {
var $name = 'Usersessions';
var $helpers = array('Html', 'Form','Ajax');
var $components = array('RequestHandler');
function store()
{
Configure::write('debug', 0);
$this->autoRender = false;
echo 'hello';
if ($this->params['url']['data'])
{
$this->data['Usersession']['data'] = $this->params['url']['data'];
$this->Usersession->Save($this->data);
echo 'Success';
}
}
}
?>
As you can see I put 'hello' before it does any evaluating. I should be able to see that in my console but I dont. I tried this method with the get and I did see the response 'hello'. Which is leaving me to the conclusion that you can not send data CROSS domain via $.post
. The only method that seems to work is getJSON()
unless someone can prove me wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不能执行普通的跨域ajax请求。您需要使用 JSONP ,这仅适用于 GET 请求(这是因为 jquery 注入了一个
script
标签添加到 DOM 以执行请求,而script
标签只能使用 GET 来获取 javascript)。You cannot perform ordinary cross domain ajax requests. You need to use JSONP and this works only with GET requests (that's because jquery injects a
script
tag to the DOM in order to perform the request and ascript
tag can only use GET to fetch javascript).如果您希望能够跨域执行请求,则需要在您的域上实现 HTTP 代理,该代理将通过服务器端实用程序/库(如 Curl 或 Apache HTTPClient 之类的。
编辑: JSONP 是一种解决方案,但我不会推荐它,除非您只需要发出 GET 请求(因为这就是所有有效的方法)。 JSONP 也不一定是 REST 友好的,特别是在您需要发出 POST 请求的情况下。如果 POST 满足您的资源的语义以及您打算如何操作它,那么仅仅为了使用 JSONP 而切换到 GET 对我来说感觉很丑陋。
If you want to be able to do requests cross-domain, you'll need to implement a HTTP proxy on your domain which would make HTTP requests on your behalf via a server side utility/library like Curl or Apache HTTPClient or something.
Edit: JSONP is a solution, but I wouldn't recommend it unless you only need to make GET requests (because that's all that works). JSONP also isn't necessarily REST-friendly, especially in your case where you need to make a POST request. If POST satisfies the semantics of your resource and how you intend to manipulate it, switching to GET just to use JSONP feels ugly to me.