2017-10-01 10 views
1

、あなたがhttp://localhost:(Serverport)に行くとき、NodeJSサーバは、次のコードを使用してHTMLファイルを送信することで応答:NodeJS、AJAX POSTからのエクスプレスショーHTMLページレスポンス

app.get('/', function(req, res){ 
    res.sendFile(__dirname + '/login.html'); 
}); 

今私が使用していますJQuery AJAX POSTはサーバーに情報を送信し、サーバーの応答に基づいてHTMLページ "/ index"またはユーザーの資格情報が正しくない場合はHTMLページ "/ loginno"にユーザーを送ります。問題は、AJAXがサーバレスポンスでsendfileのniceレスポンスを得ていないということです。

私はサーバーからファイルを取得していますが、コンソールに完全なHTMLを出力しますが、サーバーをGET応答と同じ方法でブラウザを実際にページに移動させる方法がわかりません。

これは動作し、サーバーからHTMLページオブジェクトを取得するAjaxの機能ですが、ブラウザはページに移動しません。

$.ajax({ 
     type: "POST", 
     url: '/index', //A string containing the URL to which the request is sent. 
     timeout: 2000, 

     //A plain object or string that is sent to the server with the request. 
     data: userdata, 

     //The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html). 
     dataType: 'html', 

     success: function(response,status,xhr) { 
      //show content 
      console.log('Success!' + response+', Status: '+status+', xhr: '+xhr) 
     }, 
     error: function(jqXHR, textStatus, err) { 
      //show error message 
      alert('text status '+textStatus+', err '+err) 
     } 
    }); 

だから、どのようにあなたはNodeJSサーバ応答から送信されたHTMLページのオブジェクトに移動するためにjQueryのAJAX POST機能を教えてくださいされる問題?サーバがある場合は、この質問に与えられた答えを見た後

+0

POSTに応答するには、エクスプレスでルートが必要です。既に持っている場合は、質問に含めてください。例: 'app.post( '/ index'、function(req、res)){/ * htmlページを送信* /}); ' – ThisClark

+0

ブラウザでプログラムを使って別のページを開く方法は? 'window.location.href = <あなたのajaxレスポンスからのURL>'が行います。たとえばを参照してください。 https://stackoverflow.com/questions/18634383/window-location-href-and-window-open-in-javascript – imhotap

+0

ThisClark、私はすでにルートを持っています。私の質問では、サーバーがhtmlファイルを送信していると述べました。すべてがそこに良いです。ちょうどクライアントがサーバーが送信していたhtmlファイルにナビゲートする方法を知らなかった。しかし、ありがとう。 – jwlewis

答えて

0

... Calling Response.redirect through Ajax

私は、url変数とし、Ajaxのポストの成功の内側にJSONレスポンスを構築することにより、私は必要なもののテストを行うことができましたOKを新しいページに移動し、window.location.hrefを使用してそこをナビゲートすると言っています。私のNodeJSサーバのルートで

...

app.post('/index', function(req, res) { 
    //Do some req verification stuff here 

    //If req verfiication passes 
    var servResp = {}; 
    servResp.success = true; 
    servResp.redirect = true; 
    servResp.redirectURL = "http://localhost:3000/index"; 
    res.send(servResp); 
}); 

と私のAJAXポスト機能...助けたすべての人に

$.ajax({ 
    type: "POST", 
    url: '/index', //A string containing the URL to which the request is sent. 
    timeout: 2000, 

    //A plain object or string that is sent to the server with the request. 
    data: userdata, 

    //The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html). 
    dataType: 'json', 

    success: function(response,status,xhr) { 
     //show content 
     console.log('Success!' + response+', Status: '+status+', xhr: '+xhr) 

      if(response.redirect) { 
       window.location = response.redirectURL; 
      } 

    }, 
    error: function(jqXHR, textStatus, err) { 
     //show error message 
     alert('text status '+textStatus+', err '+err) 
    } 
}); 

ありがとう!

関連する問題