path: GET /api/user/123 其中的123通过path传递
query: GET /api/search_user?userId=123
body: POST /api/user-signup {username: 'john'}
不建议通过header传参的原因:
1. proxy 和 reverse proxy会drop header
2. 不利于传输object
3. HTTP access control (CORS) API 一般会设置Access-Control-Allow-Headers,分分钟教你做人。
4. 不利于dev和debug
5. Header长度限制
然后,如果你需要传header,比如Authorization,如下。
jQuery.ajax()
headers (default: {})
Type: PlainObject
An object of additional header key/value pairs to send along with requests using the XMLHttpRequest transport. The header X-Requested-With: XMLHttpRequest is always added, but its default XMLHttpRequest value can be changed here. Values in the headers setting can also be overwritten from within the beforeSend function. (version added: 1.5)
$.ajax({ url: '/path/to/service', method: 'GET | POST | PUT | DELETE', headers: { 'Authorization': 'Bearer ', 'some-other-header': 'some value' } }) .done(function(data){...}) .fail(function(jqXHR){...}) .always(function(){...})
你好,jquery的ajax有这2个配置不知道是不是你想要的一个是headers
作用是发送一个额外的"{键:值}"对映射到请求,参数类型是object
一个是dataType
作用是设定返回数据的数据类型,参数类型是string,参考值json,script,html,text
首先首先涉及业务逻辑输入需要通参数传递主要三种:path, query, POST/PUT bodypath: GET /api/user/123 其123通path传递
query: GET /api/search_user?userId=123
body: POST /api/user-signup {username: 'john'}
建议通header传参原:
1. proxy reverse proxydrop header
2. 利于传输object
3. HTTP access control (CORS) API 般设置Access-Control-Allow-Headers钟教做
4. 利于devdebug
5. Header度限制
需要传header比Authorization
jQuery.ajax()
headers (default: {})
Type: PlainObject
An
object of additional header key/value pairs to send along with requests
using the XMLHttpRequest transport. The header X-Requested-With:
XMLHttpRequest is always added, but its default XMLHttpRequest value can
be changed here. Values in the headers setting can also be overwritten
from within the beforeSend function. (version added: 1.5)$.ajax({
url: '/path/to/service',
method: 'GET | POST | PUT | DELETE',
headers: {
'Authorization': 'Bearer ',
'some-other-header': 'some value'
}
})
.done(function(data){...})
.fail(function(jqXHR){...})
.always(function(){...})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)