<分区>
使用 https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js 我有以下方法尝试向 Google 的 URL 缩短器发送 JSON 请求
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "https://www.googleapis.com/urlshortener/v1/url",
data: { longUrl: "http://some.url" }
})
.done(function( msg ) {
console.log( msg );
});
返回 400 错误是因为请求的格式不正确。查看fiddler中的request,可以看到是这样的;
POST https://www.googleapis.com/urlshortener/v1/url HTTP/1.1
Host: www.googleapis.com
Connection: keep-alive
Content-Length: 29
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/javascript, */*; q=0.01
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
Content-Type: application/json
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6,fr;q=0.4
longUrl=http%3A%2F%2Fsome.url
我可以将请求修改为以下内容(不包括 header );
{ longUrl=http://some.url }
那么为什么 ajax 不将数据格式化为 JSON?