Fetch usage and how to receive the value transmitted by JS. fetch receives the value transmitted by js.

Source: Internet
Author: User

Fetch usage and how to receive the value transmitted by JS. fetch receives the value transmitted by js.

Basic fetch method:

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) })

Method 1: Add headers Definition

The headers are defined as follows:

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

At the same time, use the following method to pass the value to the body:

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

Use the following in php to receive

input('username')

Method 2: Change the Acceptance Method in php

The acceptance method is as follows:

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

Returns a String object. The value must be processed as follows:

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

In this case, the following values can be received:

$result['username']

Summary

The above section describes how to use fetch and how to receive JS values. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.