post 跨域问题。这次我绕不开了 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yangkeao
V2EX    问与答

post 跨域问题。这次我绕不开了

  •  
  •   yangkeao 2014-06-14 19:57:20 +08:00 7675 次点击
    这是一个创建于 4165 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想要使用多说的api,好像发表评论必须要post

    这就面临跨域问题。

    多说给的通用代码是如何解决这个问题的?我应该怎么办?


    同步异步都可以,这个没关系。
    31 条回复    2014-06-17 11:23:32 +08:00
    cbsw
        1
    cbsw  
       2014-06-14 20:28:51 +08:00
    用多说不就调用给的那段脚本就行了吗,站点静态的都行,应该不需要自己处理跨域问题
    yangkeao
        2
    yangkeao  
    OP
       2014-06-14 20:39:04 +08:00
    @cbsw 那段脚本使用时限制多多(比如不支持匿名评论),,想要自己写个,怎么处理?

    而且想知道那个脚本是怎么跨域的。
    cbsw
        3
    cbsw  
       2014-06-14 20:47:50 +08:00
    @yangkeao 这里不就可以匿名吗 http://dev.duoshuo.com/threads/51ef7387d137f48920000084 话说你是要自己写一个多说吗?
    yangkeao
        4
    yangkeao  
    OP
       2014-06-14 20:50:57 +08:00
    @cbsw 难道API里面的匿名评论指的是 可以随便打个名字??
    还以为可以不输入名字。。。

    尝试着用给的脚本,绕过本地对于输入名字的验证后提交,Bad Request。
    所以我什么都懂了。。。。

    那就算了吧。(我只是想用他的API,没有自己写一个的意思。)
    yangkeao
        5
    yangkeao  
    OP
       2014-06-14 20:51:19 +08:00
    @cbsw 那请问他的跨域是如何实现的呢?
    cbsw
        6
    cbsw  
       2014-06-14 20:56:49 +08:00
    @yangkeao 匿名评论本身就会带来很大的问题:垃圾信息。一般的网站好像不会需要完全匿名不受限制的评论吧
    yangkeao
        span class="no">7
    yangkeao  
    OP
       2014-06-14 21:02:28 +08:00
    @cbsw 嗯,好的。

    官方脚本的跨域如何实现的呢?
    cbsw
        8
    cbsw  
       2014-06-14 21:26:22 +08:00
    你是在用 django 吗,我曾经也对 django 的跨域问题比较纠结,规定表单 post 一定要加 csrf_token ,不过似乎可以通过 ajax 提交表单来绕开 django 对 post 表单的检查
    ysdj
        9
    ysdj  
       2014-06-14 23:18:40 +08:00
    在服务端调用API啊
    icanfork
        10
    icanfork  
       2014-06-14 23:20:50 +08:00
    @ysdj 对,自己的服务器做一次中转
    txlty
        11
    txlty  
       2014-06-14 23:25:23 +08:00
    抓包看了一下,跨域的秘密就在这里:


    多说API接口返回的HTTP头信息里允许跨域读取。
    Access-Control-Allow-Origin 表示允许哪个域读取信息。这个是添加站点时指定的。
    yangkeao
        12
    yangkeao  
    OP
       2014-06-14 23:31:53 +08:00
    @txlty cors吗?为什么我访问提示没有标记??
    txlty
        13
    txlty  
       2014-06-14 23:32:57 +08:00
    每个多说域名对应一个允许的站点域。

    所以,调用API时调用添加站点时设置的多说域名,比如:
    http://[你设置的多说域名].duoshuo.com/posts/create.json
    而不是
    http://api.duoshuo.com/posts/create.json
    yangkeao
        14
    yangkeao  
    OP
       2014-06-14 23:34:33 +08:00
    @txlty XMLHttpRequest cannot load http://api.duoshuo.com/posts/create.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
    yangkeao
        15
    yangkeao  
    OP
       2014-06-14 23:37:27 +08:00
    @txlty 哦!!
    那这样不方便测试呃。。。
    simonyan
        16
    simonyan  
       2014-06-14 23:41:29 +08:00
    yangkeao
        17
    yangkeao  
    OP
       2014-06-14 23:51:18 +08:00
    @txlty XMLHttpRequest cannot load http://videa.duoshuo.com/posts/create.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://idea.vdon.cn' is therefore not allowed access.

    允许的站点域是设置里面的首页网址吗?
    txlty
        18
    txlty  
       2014-06-15 00:46:14 +08:00
    @yangkeao
    试了一下,换域也可以。那就是读取的post请求时的referer。
    读取不到,可能是你提交的参数错了,返回404了。
    emric
        19
    emric  
       2014-06-15 03:11:26 +08:00
    //api.duoshuo.com/posts/create.jsonp
    使用 jsonp, 详见文档~
    yangkeao
        20
    yangkeao  
    OP
       2014-06-15 08:35:25 +08:00 via Android
    @emric POST
    emric
        21
    emric  
       2014-06-15 08:54:38 +08:00
    @yangkeao JSONP 只是跨越的一种方式, 同样是由 POST / GET 提交的...
    yangkeao
        22
    yangkeao  
    OP
       2014-06-15 08:55:56 +08:00 via Android
    @emric 一直以为jsonp只支持GET
    emric
        23
    emric  
       2014-06-15 08:59:30 +08:00
    @yangkeao 唉, 这个我错了打错. JSONP 只是 GET, 跨越请求你为什么只用 POST?
    yangkeao
        24
    yangkeao  
    OP
       2014-06-15 09:46:02 +08:00
    @emric 多说的发表评论的API必须用post不是吗?
    yangkeao
        25
    yangkeao  
    OP
       2014-06-15 09:56:59 +08:00
    @txlty 十分感谢

    抓包发现是http://[你设置的多说域名].duoshuo.com/api/posts/create.json

    后来更正后,400.
    发现是参数少了几个,只填了必选。有几个是没登录必填的,我没有,所以400了
    emric
        26
    emric  
       2014-06-15 10:10:10 +08:00
    @yangkeao jsonp 亦可以提交, IE 6 使用的是这种方法~
    yangkeao
        27
    yangkeao  
    OP
       2014-06-15 10:13:43 +08:00
    @emric
    reorx
        28
    reorx  
       2014-06-15 12:10:14 +08:00
    @cbsw 没用过多说不知道它具体怎么做,但跨域是 HTTP 协议上的东西,跟框架没有关系,现在无外乎两种方式: jsonp 和 CORS,后者请查看 mozilla 文档 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

    你说的那个 csrf_token 是所谓的跨站请求伪造,为了给模拟登录和其他请求增加难度,和跨域不一样的。
    yangkeao
        29
    yangkeao  
    OP
       2014-06-15 12:24:00 +08:00
    @reorx 多说使用了CORS,之前我因为请求地址错误了的问题才没访问到。
    HunterPan
        30
    HunterPan  
       2014-06-15 18:37:40 +08:00
    jsonp?
    yangkeao
        31
    yangkeao  
    OP
       2014-06-17 11:23:32 +08:00
    @HunterPan 标题上已经说了post
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2645 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 14:20 PVG 22:20 LAX 06:20 JFK 09:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86