我想用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
});
}
})
巴扎黑2017-04-10 14:37:02
$.post()
不支持加入 headers
信息,改用 $.ajax()
。demo 如下:
function getCode(param, config, notice) {
var url = config.apiBase
+ "/resource/";
var data = {
"name": param.name,
"user_agent": param.ua,
"mobile": $("#phone").val()
};
$.ajax({
url: url,
type: 'post',
data: data,
headers: {
"X-Requested-Accept": 'json'
},
dataType: 'json',
success: 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
});
}
}
});
}
高洛峰2017-04-10 14:37:02
$.ajax({
type: 'POST',
url: "http://kycool-girl.com/head.php",
headers: {
"access":"json",
}
}).done(function(data) {
console.log(data);
});
请求的头
Remote Address:127.0.0.1:80
Request URL:http://kycool-girl.com/head.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8,en;q=0.6
access:json
Cache-Control:no-cache
Connection:keep-alive
Content-Length:0
Host:kycool-girl.com
Origin:http://127.0.0.1:8000
Pragma:no-cache
后端简单设置 PHP 文件
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description, access');
$data = array("name"=>"allen", "age"=>27);
echo json_encode($data);
天蓬老师2017-04-10 14:37:02
$.ajax({ url: url,
type: "POST",
data: data,
beforeSend: function(req) {
req.setRequestHeader("Accept", "application/json");
},
success: function(json) {
// do something...
},
error: function(xhr) {
// do something...
}});
阿神2017-04-10 14:37:02
$.ajax({url: ajaxUrl,
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=UTF-8',
data: JSON.encode(params),
}).then(function(data) {
return data;
});
http://api.jquery.com/jQuery.ajax/
看文档,
contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')
Type: String
When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding. Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.
这一段的介绍,可以在配置参数中,修改传输的格式,默认是 'application/x-www-form-urlencoded; charset=UTF-8'。
怪我咯2017-04-10 14:37:02
$.post(url,$.noop,'json')
$.post(url,postDataObject,$.noop,'json')
$.post(url,postDataString,$.noop,'json')