Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React Js:Uncaught(承诺中)Syntaxer错误:_Javascript_Json_Reactjs_Fetch - Fatal编程技术网

Javascript React Js:Uncaught(承诺中)Syntaxer错误:

Javascript React Js:Uncaught(承诺中)Syntaxer错误:,javascript,json,reactjs,fetch,Javascript,Json,Reactjs,Fetch,我正在尝试通过reactjs中的fetch执行POST请求。我看了一些文件,但我的错误没有解决。有人能帮我吗 以下是我的代码: handleSubmit(e) { e.preventDefault(); var self = this; const payload = { id: 111, studentName: 'param', age: 24, emailId: 2 }; fetch({ method: 'POST', url: 'h

我正在尝试通过reactjs中的
fetch
执行POST请求。我看了一些文件,但我的错误没有解决。有人能帮我吗

以下是我的代码:

handleSubmit(e) {
   e.preventDefault();
   var self = this;

   const payload = {
   id: 111,
   studentName: 'param',
   age: 24,
   emailId: 2
};
fetch({
   method: 'POST',
   url: 'http://localhost:8083/students',
   body: payload,
   headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
 })
   .then(function(response) {
       return response.json()
     }).then(function(body) {
       console.log(body);
     });
 }
}

如果有人熟悉reactjs,只需给出一个简单的例子,说明如何调用post请求。可以使用
fetch
axios

下面是一个例子

  fetch('http://myAPiURL.io/login',{
    method:'POST',
    headers:{
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    body:JSON.stringify({
    email: userName,
    password: password
  })
  }).then(function (response) {

    //Check if response is 200(OK) 
    if(response.ok) {
      alert("welcome ");
    }
    //if not throw an error to be handled in catch block
    throw new Error(response);
  })
  .catch(function (error) {
    //Handle error
    console.log(error);
  });

for more info on how to use `fetch` https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
下面是一个例子

  fetch('http://myAPiURL.io/login',{
    method:'POST',
    headers:{
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    body:JSON.stringify({
    email: userName,
    password: password
  })
  }).then(function (response) {

    //Check if response is 200(OK) 
    if(response.ok) {
      alert("welcome ");
    }
    //if not throw an error to be handled in catch block
    throw new Error(response);
  })
  .catch(function (error) {
    //Handle error
    console.log(error);
  });

for more info on how to use `fetch` https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

我刚才回答了一个类似的问题

React最棒的地方在于它只是Javascript

所以,你所需要的只是做一件事后做你的服务器

您可以使用本机函数或完全打开库,如

使用这两种方法的例子可以是:

// Using ES7 async/await
const post_data = { firstname: 'blabla', etc....};
const res = await fetch('localhost:3000/post_url', { method: 'POST', body: post_data });
const json = await res.json();



我刚才回答了一个类似的问题

React最棒的地方在于它只是Javascript

所以,你所需要的只是做一件事后做你的服务器

您可以使用本机函数或完全打开库,如

使用这两种方法的例子可以是:

// Using ES7 async/await
const post_data = { firstname: 'blabla', etc....};
const res = await fetch('localhost:3000/post_url', { method: 'POST', body: post_data });
const json = await res.json();



你能编辑问题吗,代码缺失?@niklaz谢谢你的回答,代码添加了你能编辑问题吗,代码缺失了?@niklaz谢谢你的回答,代码添加了为了得到一个好的答案,我建议对你的代码添加更多的解释,让事情更清楚。@Sandeep谢谢你的回答,我尝试过这种方法,但出现了相同的错误为了得到一个好的答案,我建议您在代码中添加更多的解释,以使事情更清楚。@Sandeep谢谢您的回复,我尝试过这种方法,但出现了相同的错误