将变量从 JavaScript 传递到 php(通过获取 api)

passing variables from JavaScript to php (through fetch api)

我正在尝试 post 我的变量 javascript 到 php 文件。 我无法 post 变量,但能够传递 json 数据。

我的代码是这样的。

     var check = 'hello';
     var check1 = 'hello1';

    fetch('http://localhost/react_task/react-webpack-boilerplate/php/api.php', {
        method: 'post',
        body: JSON.stringify({
            a: 2, // here passing variable doesn't work 
            b: 1  // like check and check1 does't show any values while passing here
        })
    }) .then(function(response) {
            if (response.status >= 200 && response.status < 300) {
                return response.text()
            }
            throw new Error(response.statusText)
        })
        .then(function(response) {
            console.log(response);
    }) 

你能指导我吗,如何将变量从 JavaScript 传递到 php。

你可以使用 ajax 来做到这一点: 举个例子 ;;;

var a=10;
var b=20;
$.post('abc.php',{param1:a,param2:b},function(data){
 //here action on data returned or anything
});