我想用js发送post消息给服务器,并且要求返回json格式的数据,我查了一下jquery的文档,但是还是没办法把accept=json加入到header中。代码如下
function getCode(param, config, notice){
var url = config.apiBase
+ "/resource/";
var data = {
"name": param.name,
"user_agent": param.ua,
"mobile": $("#phone").val()
};
$.post(url, data, )
.done(function(data){
console.log(data);
if(data.ret){
notice.showMsg({
msg: "短信验证码已经发送。",
type: "info",
deley: 0,
duration: 0,
close: true
});
countDown();
}else{
notice.showMsg({
msg: "短信验证码发送失败。",
type: "error",
deley: 0,
duration: 0,
close: true
});
}
})
$.post()
不支持加入headers
信息,改用$.ajax()
。demo 如下: