jquery通过A$.post发送跨域数据

发布于 2024-08-10 05:08:47 字数 1258 浏览 6 评论 0原文

我正在尝试通过 $.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尽揽少女心 2024-08-17 05:08:47

不能执行普通的跨域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 a script tag can only use GET to fetch javascript).

一笑百媚生 2024-08-17 05:08:47

如果您希望能够跨域执行请求,则需要在您的域上实现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文