ajax php token,thinkphp5 ajax提交token更新的问题

在使用thinkphp5做编辑删除的ajax操作时,携带token。点击第一次时验证器提示成功,点击第二次时提示无效的令牌。后来发现原因是因为var token='{$Request.token}';这里的值没变所以导致令牌验证失败。现在在控制器中$request->token();重新生成了token并已header形式返回到客户端,但是如何让ajax提交时的token为新的值呢?我的代码改如何修改,谢谢

附:我的代码:

html部分:

1yxg$1

编辑

删除

jquery部分:

function editOne(url,id){

// 第一生成token

var token='{$Request.token}';

var xhr=$.ajax({

url:url,

type:"POST",

dataType:"text",

data:{'id':1,'__token__':token},

})

.done(function(data) {

// token=xhr.getResponseHeader("__token__");

console.log(data);

console.log(xhr.getResponseHeader("__token__"));

})

.fail(function() {

alert(data.msg);

})

.always(function() {

console.log("complete");

});

}

thinkphp控制器部分:

public function delmulti(){

$request = \think\Request::instance();

$data=$_POST;

$validate=new Validate([

'id'=>'require|token',

]);

if(!$validate->check($data)){

// 重新生成token返回

$request->token();

$res['msg']=$validate->getError();

$jres=json_encode($res,JSON_UNESCAPED_UNICODE);

echo($jres);

}else{

// 重新生成token返回

$request->token();

$res['msg']='success';

$jres=json_encode($res,JSON_UNESCAPED_UNICODE);

echo($jres);

}

}

你可能感兴趣的:(ajax,php,token)