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

passing variables from JavaScript to php (through fetch api)

本文关键字:通过 fetch api php 变量 JavaScript      更新时间:2023-09-26

我试图张贴我的变量javascript到php文件。我不能够发布变量,但能够传递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
});