2016-06-15 5 views
0

私はフォームを送信するときにwebappsとjavascriptとimに新しいと私に感謝のページに行くことを試みています。しかし、空のページに行くだけです。私はどのように見えたが、何も働いていないと思う。私はres.end()で何かをしなければならないことを知っています。なぜなら、それを置かないと、インデックスページがロードシンボルを継続的に作成するからです。フォームを送信するときに新しいhtmlページに移動

どのような提案も素晴らしいでしょう!ありがとうございました。

あなたがやりたい私のindex.htmlで

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Thank you</title> 
</head> 
<body> 
<h1>Thank you!!!!!</h1> 
</body> 
</html> 

私のフォームセクション私server.jsの

<div class=container2> 
      <form method="post" action="/thankyou.html" enctype="multipart/form-data" autocomplete="off" > 
       <fieldset> 
        // all my inputs and selectors 
        <input type="submit" value="Submit"> 
       </fieldset> 
      </form> 
     </div> 

部分(ノード)

var express = require('express'); 
var path = require('path'); 
var server = express(); 
var formidable = require("formidable"); 
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; 
var xhr = new XMLHttpRequest(); 
var request = require("request"); 

server.use(function(req, res, next) { 
    res.header("Access-Control-Allow-Origin", "*"); 
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 
    res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE,OPTIONS'); 
    next(); 
}); 


var url = '*****************************'; 

var port = process.env.port || 63342; 

// Define ./public as static 
server.use(express.static(path.join(__dirname, 'public'))); 


server.post("/thankyou.html", function(req, res, next) { 
    processFormFieldsIndividual(req, res); 
}); 


//All POST's use processFormFieldsIndividual 
//server.post('*', processFormFieldsIndividual); 

server.listen(port, function() { 

    console.log('listening on port ' + port); 
}); 


function processFormFieldsIndividual(req, res) { 
    // take the values from the form and store it to 
    // the schema for patient. 

    var form = new formidable.IncomingForm(); 
    form.on('field', function (field, value) { 


     switch (field) { 
      //puts values in json 

    }); 

    form.on('end', function() { 
     res.writeHead(200, { 
      'content-type': 'text/plain' 
     }); 

     // checks if the exists before it does a put or post. 
     var exists = checkIfExist(schema.name.family, schema.birthDate); 


     // if exists do a put 
     if (exists) { 
      res.end(); 
      xhr.open("PUT", url, true); 
      xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 
      xhr.onreadystatechange = function() { 
       if (xhr.readyState == 4 && (xhr.status == 201 || xhr.status == 200)) { 
        console.log(xhr.responseText); 
       } 
       else 
        console.log(xhr.responseText); 
      }; 
      xhr.send(JSON.stringify(schema)); 
     } 
     // if doesn't exist do a post 
     else { 
      res.end(); 
      xhr.open("POST", url, true); 
      xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 
      xhr.onreadystatechange = function() { 
       if (xhr.readyState == 4 && (xhr.status == 201 || xhr.status == 200)) { 
        console.log(xhr.responseText); 
       } 
      }; 
      xhr.send(JSON.stringify(schema)); 
     } 
     console.log(JSON.stringify(schema)); 
    }); 
    form.parse(req); 
} 

答えて

2

thankyou.html処理が完了したらリダイレクトします。この質問を参照してください:Nodejs - Redirect url希望の成功ページにリダイレクトするだけです。あなたの成功ページがフォームにある場合

/Congratulations.htmlは、あなたのリダイレクトは、次のようになります。

res.redirect('forms/Congratulations.html'); 

はあなたres.end()を削除し、あなたのロジックの最後にリダイレクトを置きます。

xhr.send(JSON.stringify(schema)); 
res.redirect('forms/Congratulations.html'); 
1

投稿した説明とコードに基づいて、少し複雑になっているように見えます。あなたは次のことをやってみません理由:

1)そのように、単にデータを処理するためのエンドポイントを持っている:

server.post('/process-form', function(req, res, next) { 
    processFormFieldsIndividual(data); 
}); 

2)あなたは以下のように、その後にユーザーをリダイレクトすることができ、エンドポイントを持っていますそう:

server.post('/process-form', function(req, res, next) { 
    processFormFieldsIndividual(data).then(function() { 
     res.redirect('/thankyou.html'); 
    }); 
}); 
processFormFieldsIndividualが非同期である

server.post('/process-form', function(req, res, next) { 
    processFormFieldsIndividual(data); 
    res.redirect('/thankyou.html'); 
}); 

場合、それは、代わりにあなたが行うことができ、そのように約束を返してきました

私はそれが助けてくれることを願っています!

+0

あなたの懸念に対処した場合は、この回答を正しいとマークすることを検討してください(この投稿の左上にあるグレーのチェックマークをクリックしてください)。 –

0

ありがとうございます!両方のソリューションが機能します。また、私は私が

res.writeHead(200, { 
      'content-type': 'text/plain' 
     }); 

、すべての

res.end(); 

SOLUTION

server.post("/process-form", function(req, res, next) { 
    processFormFieldsIndividual(req, res); 
    res.redirect('/thankyou.html') 
}); 

インデックスを削除したパブリックフォルダ(クライアント側)

にthankyou.htmlを移動しなければならなかったします.html

<div class=container2> 
      <form method="post" action="/process-form" enctype="multipart/form-data" autocomplete="off" > 
       <fieldset> 
        // all my inputs and selectors 
        <input type="submit" value="Submit"> 
       </fieldset> 
      </form> 
     </div> 
関連する問題