fetch 函数传递参数与后台交互

介绍fetch函数:fetch( input [,init ]).then(function(response){ })

input定义要获取的资源。这可能是:

  • 一个 USVString 字符串,包含要获取资源的 URL。一些浏览器会接受blob:和data: 作为 schemes.
  • 一个 Request 对象。

init 可选:

一个配置项对象,包括所有对请求的设置。可选的参数有:

  • method:请求使用的方法,如GET、POST
  • headers:请求的头信息,形式为 Headers的对象或包含 ByteString值的对象字面量。
  • body:请求的 body 信息:可能是一个 BlobBufferSourceFormDataURLSearchParams或者 USVString 对象。注意 GET 或 HEAD 方法的请求不能包含 body 信息。cors no-cors 或者 same-origin
  • mode:请求的模式,如cors 、no-cors 或者 same-origin。
  • credentials: 请求的 credentials,如 omit、same-origin或者include。为了在当前域名内自动发送cookie,必须提供这个选项。
  • cache:请求cache模式:default、no-store、reload、no-cache、fore-cache 或者 only-if-cached。
  • redirect: 可用的 redirect 模式: follow(自动重定向), error(如果产生重定向将自动终止并且抛出一个错误), 或者 manual(手动处理重定向). 在Chrome中,Chrome 47之前的默认值是 follow,从 Chrome 47开始是 manual。
  • referrer: 一个 USVString 可以是 no-referrer、client或一个 URL。默认是client
  • referrerPolicy: Specifies the value of the referer HTTP header. May be one of no-referrer no-referrer-when-downgrade、origin origin-when-cross-origin、UNsafe-url。
  • integrity: 包括请求的  subresource integrity 值 ( 例如: sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=)。

返回值:一个 Promise,resolve 时回传 Response 对象。

示例:与后台交互并传递参数(这里只是说明,要访问,请自行更改)

前端:

fetch(‘www/getData’, {method:'POST'
                      ,headers:{ 'Content-Type': 'application/json' }
                      ,body: JSON.stringify({ key: value,key1:value1...... })
                   }).then( function (resp) {
                      console.log(resp);
                      return resp.blob() })

访问的路径:URL= 'www/getData'

请求的方法: POST

传递多个参数:JSON.stringify({ key: value,key1:value1...... })

后台:以java为例

@RequestMapping(value = "getData", method = RequestMethod.POST)
public Object getData(@RequestBody Map<String, Object> map){
	
}

参考链接:

  1. https://github.com/github/fetch
  2. https://github.github.io/fetch/
  3. https://github.com/developit/unfetch
  4. https://github.com/elbywan/wretch
  5. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

可以从map.get("key") 对应的value值。。。。。。;

记得要用:@RequestBody 

@RequestParam 和 @PathVariable 不可以

想想就知道
 

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
前端中,可以通过使用JavaScript来实现表单的提交,并且可以将表单据转换成JSON格式,并通过AJAX技术向后台发送POST请求。下面是一个简单的例子: ```html <form id="my-form"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <input type="submit" value="Submit"> </form> <script> // 获取表单元素 const form = document.getElementById('my-form'); // 监听表单提交事件 form.addEventListener('submit', handleSubmit); // 处理表单提交事件 function handleSubmit(event) { // 阻止表单默认提交行为 event.preventDefault(); // 获取表单据 const formData = new FormData(event.target); const json = JSON.stringify(Object.fromEntries(formData.entries())); // 向后台发送请求 fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: json }) .then(response => response.text()) .then(data => { // 处理响应 alert(data); }) .catch(error => { // 处理错误 alert(error); }); } </script> ``` 在这个例子中,我们首先获取了表单元素,并通过addEventListener函数监听表单提交事件。当表单提交时,我们通过preventDefault函数阻止了表单的默认提交行为,并通过FormData对象获取了表单据。然后,我们将表单据转换成JSON格式,并使用fetch函数后台发送POST请求。发送请求后,我们通过then函数处理响应,并将其作为弹窗显示给用户。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值