PHP网站
我的代码如下,如何禁止直接访问ajax页面,限定只接收post请求
如果 ajax 的 type 设为了 post 本身就只能接受post 请求。
本身就只能接受post 请求。
type: 'POST',只是限制了客户端的请求,但是直接打开请求的url,是直接请求服务端,所以说服务端也需要限制为post方式访问,不清楚使用
的是php的那种框架,所以无法给出服务端代码
加一个过滤器,如果http 头中 x-requested-with值为XMLHttpRequest,则放行,否则过滤。望采纳。
判断来源,直接访问来源为空
Request.ServerVariables["HTTP_REFERER"]
通过Request.ServerVariables["Request_Method"] 判断请求方法,不是post直接结束程序的输出
后台用的啥?
如果是java springMVC, 在controller层限制即可
@PostMapping("/timeout")
public String timeout(){
return "timeout";
}
这样如果是get请求,就请求不成功了
java web全栈开发
$.ajax是浏览器的技术,肯定是无法实现,需在服务器端限制。
如下,在Spring MVC中,使用RequestMapping注解时,设置method=RequestMethod.POST。
@RequestMapping(value="/doSomething", method=RequestMethod.POST)
public void doSomething(
@RequestParam(value="page") int page,
@RequestParam(value="rows") int rows
) throws Exception {
//
}
可以在js前端和后台同时限制,如果是后台限制 则 在Spring MVC中,使用RequestMapping注解时,设置method=RequestMethod.POST
type : post 就是只能接受post请求啊
post 本身就只能接受post 请求,是java springMVC, 在controller层限制即可