如何邮差发送请求? AJAX,同源策略 [英] how Postman send requests? ajax, same origin policy

查看:202
本文介绍了如何邮差发送请求? AJAX,同源策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个非常有用的Chrome扩展名为邮递员,这是一个非常有用的扩展尤其是当你进入编程RESTful应用程序。

有一件事情我感到困惑的是,如何这个插件/扩展能够成功地发送POST请求不同的域?

我试图用邮差这样的投票方式进行表决。

提交的错误后,投票实际上计数,但是当我试图这样做,使用Ajax和JavaScript,它失败,因为浏览器不同产地的政策。

这怎么可能呢?

下面是我使用jQuery code。我曾经在我的电脑,虽然,本地主机。

 初始化:函数(){
    $阿贾克斯({
        网址:http://domain.com/vote.php,
        键入:POST,
        数据类型:HTML,
        数据: {
            ID:'1'
        },
        成功:功能(数据){
        如果(数据=='投票'){
            $('。设定结果)HTML('您已经投尝试后再次24小时)。
        } 其他 {
            $('。设定结果)HTML('成功投)。
        }
    }
    });
},
 

解决方案

Chrome浏览器打包应用程序可以跨域权限。当您安装邮差它promts你,这个程序将访问任何域。

通过将你的manifest文件* / * 权限部分,你可以做到这一点。

在这里阅读更多: <一href="https://developer.chrome.com/extensions/xhr.html">https://developer.chrome.com/extensions/xhr.html

I have found this very useful chrome extension called postman, this is a very useful extension especially when you are into programming RESTful applications.

One thing I am confused on is that how this plugin/extension able to send POST request successfully on different domains?

I tried voting in a poll using Postman like this.

After submitting that, the vote was actually counted in, but when I tried doing that using ajax and javascript, it fails, because of different origin policy of browsers.

How is that even possible?

Here is my code using jquery. I used that in my computer though, localhost.

init: function() {
    $.ajax({
        url: 'http://domain.com/vote.php',
        type:'POST',
        dataType: 'html',
        data: {
            id: '1'
        },
        success: function(data) {
        if ( data == 'voted' ) {
            $('.set-result').html( 'you already voted. try again after 24 hours' );
        } else {
            $('.set-result').html( 'successfully voted' );
        }
    }
    });
},

解决方案

Chrome packaged apps can have cross domain permissions. When you install Postman it promts you that this app will access any domain.

By placing */* in permissions section of your manifest file, you can do this.

Read more here: https://developer.chrome.com/extensions/xhr.html

这篇关于如何邮差发送请求? AJAX,同源策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆