fetch 使用及如何接收JS傳值,fetch接收js傳

來源:互聯網
上載者:User

fetch 使用及如何接收JS傳值,fetch接收js傳

使用fetch基本方式:

fetch('https://mywebsite.com/endpoint/', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json'}, body: JSON.stringify({ username: 'username', password: 'password'}) }).then(function(res){ console.log(res) })

方式一:增加headers 定義

在headers頭部定義如下:

headers: {'Content-Type': 'application/x-www-form-urlencoded'},

同時body傳值使用如下方式:

body:'username='+uname+'&password='+password

在php中使用如下接收

input('username')

方式二:改變php中接受方式

接受方式如下:

$arr = file_get_contents("php://input");

返回字串對象,使用值需要做如下處理:

$result=array(); foreach (explode('&', $arr) as $t){ list($a,$b)=explode('=', $t); $result[$a]=$b; }

此時便可以如下接收傳值:

$result['username']

總結

以上所述是小編給大家介紹的fetch 使用及如何接收JS傳值,希望對大家有所協助,如果大家有任何疑問歡迎給我留言,小編會及時回複大家的!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.