2017-01-08 90 views
0

我正在尝试使用superagent发出post请求,但它总是失败。我已经尝试了很多来自互联网的解决方案,包括添加多个头文件,但似乎都没有工作。代码:React中的superagent发送请求失败

createUser() { 
var email = this.refs.email.value; 
Request.post('http://advacedcrm.local/api/users/create').set('Accept', 'application/json').send({email: email}).then((response) => { 
    console.log(response); 
}); 

}

我不得不提的是,API也是我做的,我把一些标题有:

header('Access-Control-Allow-Origin: http://localhost:3000'); 

routes.php文件的开始(在Laravel 5.2)。 的API中间件:

public function handle($request, Closure $next) 
{ 
    if ($request->isMethod('options')) { 
     return response('', 200) 
      ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE') 
      ->header('Access-Control-Allow-Headers', 'accept, content-type, x-xsrf-token, x-csrf-token'); 
    } 

    return $next($request); 
} 

但是,当我将它张贴,错误我收到:

XMLHttpRequest cannot load http://advacedcrm.local/api/users/create. Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response. 

所以我的想法,有什么不好?

回答

2

可以安装的SuperAgent

npm install superagent --save 

然后做专呼叫服务器

import request from "../../node_modules/superagent/superagent"; 

request 
.post('http://localhost/userLogin') 
.set('Content-Type', 'application/x-www-form-urlencoded') 
.send({ username: "username", password: "password" }) 
.end(function(err, res){ 
console.log(res.text); 
}); 
相关问题