jquery ajax怎么通过header传递参数

jquery ajax怎么通过header传递参数,第1张

首先的首先,涉及业务逻辑的输入是需要通过参数传递的,主要有三种方法:path, query, POST/PUT body

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 body

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 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(){...})


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11542142.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存