6

ajax请求发出去以后后端没有日志,也没有走到断点

 3 years ago
source link: http://blog.ilibrary.me/2019/11/05/ajax%E8%AF%B7%E6%B1%82%E5%8F%91%E5%87%BA%E5%8E%BB%E4%BB%A5%E5%90%8E%E5%90%8E%E7%AB%AF%E6%B2%A1%E6%9C%89%E6%97%A5%E5%BF%97-%E4%B9%9F%E6%B2%A1%E6%9C%89%E8%B5%B0%E4%B8%8D%E5%88%B0%E6%96%AD%E7%82%B9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ajax请求发出去以后后端没有日志,也没有走到断点 | 垂钓江湖
欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me

在调试一个ajax请求的时候发现put, delete方法不工作。chrome后台调试发现请求是发出去了的。 但是在后端没有任何日志,也没有任何断点断上,好像没有没有发生请求一样,但是chrome明明是发了请求的。陷入迷之困境出不来。

偶然发现get api每次会发两个请求,第一个是options, 第二个才是get。才想起我的ajax是有跨域的(port不一样), 每次跨域请求出去之前会先发一个options请求给服务器,看服务器是否支持跨域,是否支持对应的方法。

我用了gem rack-cors做跨域请求支持,解决方法就是修改config/application.rb, Rack::Cors部分修改如下:

    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', headers: :any, methods: [:get, :post, :put, :delete, :patch, :options] # 重点在这里,把支持的methods都加上
      end
    end if Rails.env.development?

重启服务器,问题解决!

扫描二维码分享到微信朋友圈Loading...Please waitqrcode.php?url=%2F2019%2F11%2F05%2Fajax%25E8%25AF%25B7%25E6%25B1%2582%25E5%258F%2591%25E5%2587%25BA%25E5%258E%25BB%25E4%25BB%25A5%25E5%2590%258E%25E5%2590%258E%25E7%25AB%25AF%25E6%25B2%25A1%25E6%259C%2589%25E6%2597%25A5%25E5%25BF%2597-%25E4%25B9%259F%25E6%25B2%25A1%25E6%259C%2589%25E8%25B5%25B0%25E4%25B8%258D%25E5%2588%25B0%25E6%2596%25AD%25E7%2582%25B9

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK