weixin_33709590 2020-03-26 17:18 采纳率: 0%
浏览 63

AJAX POST与GET

I'm wondering if it really makes any difference if I use GET or POST in my AJAX calls.

I'm using AJAX to delete an item with a specified ID. Somehow, POST seemed like it was a more fitting choice. But with Razor Pages, an AJAX POST requires several steps to work around cross site request forgery measures.

Does it make any difference if I just GET or POST here? Or are there any anti patterns I'm hitting if I don't?

  • 写回答

1条回答 默认 最新

  • 笑故挽风 2020-03-26 17:42
    关注

    There are many perspectives from which to answer your question "Does it make any difference if I just GET or POST here?" In short, YES it makes a difference.

    GET and POST are both susceptible to CSRF attacks. By using a GET you are creating a wider attack surface. For example an IMG tag in an email could run your DELETE api on an unsuspecting user. Using a POST makes it less trivial. You'd be better off using post without supporting anti-forgery than by using GET. Ideally, you're using POST with anti-forgery tokens.

    Other considerations can include:

    • How the cache handles this request
    • Adherence to REST principles (assuming you intend to have a "REST"ful/like design)
    • It's usually assumed that a get doesn't directly manipulate server side state
    • In theory a URL identifies the location of a resource, if you delete that resource with a get, what happens next time you call the get?
    • A get won't be protected by CORS because it's assumed to not modify state, the browser will execute the GET and just not return the response to the offending site. With a POST the browser will run a CORS preflight check using the OPTIONS verb to make sure the request is allowed by the CORS policy returned by your server prior to your server receiving a request with the verb POST.
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看