2016-09-09 56 views
0

我想将我的变量javascript发布到php文件。 我不能发布变量,但能够传递json数据。将变量从JavaScript传递到php(通过获取api)

我的代码是这样的。

 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。

回答

0

雅你可以使用ajax: 只是一个例子;;;

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

我与工作发生反应,JS和文件类型JSX工作..所以它很难使用JSX文件格式 –

+0

阿贾克斯,我只希望使用取... –