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

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

问题描述

我发现了一个非常有用的 Chrome 扩展程序 Postman.这是一个非常有用的扩展,尤其是在您编写 RESTful 应用程序时.

I have found this very useful Chrome extension called Postman. This is a very useful extension especially when you are into programming RESTful applications.

让我感到困惑的一件事是,此插件/扩展程序如何能够在不同域上成功发送 POST 请求?

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

我尝试像这样使用 Postman 在民意调查中投票.

I tried voting in a poll using Postman like this.

提交后,投票实际上被计入了,但是当我尝试使用 AJAX 和 JavaScript 这样做时,它失败了,因为浏览器的来源策略不同.

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.

这怎么可能?

这是我使用 jQuery 的代码.我在我的电脑中使用了它,localhost.

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

init: function() {
    $.ajax({
        url: 'http://example.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 打包应用可以具有跨域权限.当您安装 Postman 时,它会提示您此应用将访问任何域.

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

通过将 */* 放在清单文件的 permissions 部分,您可以做到这一点.

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

在此处阅读更多信息:https://developer.chrome.com/extensions/xhr.html

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

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