首页 > 代码库 > ajax设置自定义请求头信息
ajax设置自定义请求头信息
客户端请求
$.ajax({ type:"post", url:urlstr, dataType:‘json‘, async:true, headers:{token:‘abck‘}, /* beforeSend: function (xhr) { //发送ajax请求之前向http的head里面加入验证信息 xhr.setRequestHeader("token", abck); // 请求发起前在头部附加token },
*/ success:function (data) { console.log(data.info); console.log(JSON.stringify(data)); }, error:function (xhr,text) { alert(text); } });
服务端代码
public function test_jsonp() { header("Access-Control-Allow-Headers: token"); $arr = getallheaders(); foreach($arr as $key=>$val){ $data[$key] = $val; } // echo $callback .‘(‘ . json_encode($data) . ‘)‘; echo json_encode($data); exit; /* * 或者可以这样合并写 exit($callback .‘(‘ . json_encode(array(‘info‘=>‘jsonp test success‘)) . ‘)‘); * */ }
ajax设置自定义请求头信息
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。