2017-01-08 8 views
0

superagentpostリクエストを作成しようとしていますが、常に失敗します。私は複数のヘッダーを追加するなど、インターネットから多くのソリューションを試しましたが、どれもうまくいかないようです。コード:Reactでのスーパーエージェントによる投稿要求が失敗しました

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); 
}); 
関連する問題