Differentiating Between an AJAX Call / Browser Request

前端 未结 2 1260
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 19:18

Is there anything in the header of an HTTP request that would allow me to differentiate between an AJAX call and a direct browser request from a given client? Are the user a

相关标签:
2条回答
  • 2020-12-02 20:10

    If you use Prototype, jQuery, Mootools or YUI you should find a X-Requested-With:XMLHttpRequest header which will do the trick for you. It should be possible to insert whatever header you like with other libraries.

    At the lowest level, given a XMLHttpRequest or XMLHTTP object, you can set this header with the setRequestHeader method as follows:

    xmlHttpRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    
    0 讨论(0)
  • After some research, it looks like the best approach would be to simply specify a custom user agent string when making AJAX calls and then checking for this custom user agent string on the server.

    0 讨论(0)
提交回复
热议问题