2016-10-21 3 views
0

ここに私のコードがありますが、 ".then"部分はトリガーされません。どうすれば ".then"をトリガーできますか?それが「.then」に入る唯一の時間は、フォームが空のときに提出するときです。Fetch API投稿が決して落ちません。

HTML:

<form> 
       <input name="username" class="no-top input" type="text" placeholder="username"></input> 
       <input name="password" class="input" type="password" placeholder="password"></input> 
       <div class="table"> 
        <div class="table-row"> 
         <div class="table-cell"> 
         <button class="button" type="submit" value="Login" onclick="post.account.login()"></button> 
         </div> 
         <div class="table-cell"> 
         <div class="box">Forgot password?</div> 
         <div class="box">Request access</div> 
         </div> 
        </div> 
       </div> 
       </form> 

JAVASCRIPT:

const request = new Request('http://localhost:8000/account/access', { 
     method: 'POST', 
     headers: header, 
     redirect: 'manual' 
     body: JSON.stringify({ 
      email_address: document.getElementsByName('email_address')[0].value, 
      created_date: now() 
     }) 
     }) 
    fetch(request).then((response) => { 
    alert('test') 
    if (response.status >= 400) throw new Error("Bad response from server") 
    if (response.status == 200) 
     return response.json() 
    }).then((response) => { 
    console.log('success') 
    }) 
+0

に関するいくつかのエラーは、その後、応答でバックウル要求されたURLと自動的にUをトリガーする参照賭けます手動でトリガーする必要がある –

+1

チェーンの最後に '.catch(e => console.log(e))'を追加するとエラーが表示されます(Webデベロッパーツールコンソールとネットワークタブもチェックします) - 私の推測それはCORSの問題です –

+0

申し訳ありませんが、手動でトリガーする必要はありません。 –

答えて

0

これを試してみてください。..

var myRequest = new Request('demo.php', {method: 'POST', body: '{"foo":"bar"}'}); 

    fetch(myRequest) 
.then(function(response) { 
    if(response.status == 200) //return response.json(); 
    console.log(response); 
    else console.log('Something went wrong on api server!'); 
}) 
.catch(function(error) { 
    console.error(error); 
}); 
+0

それに落ちたが、停止したランダムにペダルに落ちて。 – Jon

0

をあなたがそう

const request = new Request('http://localhost:8000/account/access', { 
    method: 'POST', 
    headers: header, 
    redirect: 'manual' 
    body: JSON.stringify({ 
     email_address: document.getElementsByName('email_address')[0].value, 
     created_date: now() 
    }) 
}) 
fetch(request).then((response) => { 
    alert('test') 
    if (response.status >= 400) throw new Error("Bad response from server") 
    if (response.status == 200) 
     return response.json() 
}).then((response) => { 
    console.log('success') 
}).catch(function(err) { 
    console.log(err); 
}); 
よう.thenチェーンに.catchを追加した場合

あなたのブラウザ開発ツールのコンソールで見て、エラーがまた

何であるかが表示されます - 私はあなたがCORS

+0

それは厄介です。私はデータベースに投稿していますが、私はcorsエラーを取得していません。しかし、気になることは '.then'に落ちていません – Jon

+0

それは.catchに落ちています - つまり、コンソールに記録されているものです –

関連する問題