2017-02-09 88 views
0

我有一個表格,我要求客戶輸入幾個細節,我怎樣才能得到響應從Web服務器

<form id="redirectForm" accept-charset="UTF-8" method="post" action="https://test.test.com//api/v1/order/create" > 
    customerName: <input type="text" name="customerName" value=<%=customerName %> /> 
    customerEmail: <input type="text" name="customerEmail" value=<%=customerEmail %> /><br><br> 
customerPhone: <input type="text" name="customerPhone" value=<%=customerPhone %> /><br><br> 
signature:<input type="text" name="signature" value=<%=signature %>  /> 

在根據形式的行動提交頁面重定向和顯示JSON型響應(狀態來+付款鏈接)。 迴應是這樣的:

{"status":"OK","paymentLink":"https:\/\/test.test.com\/billpay\/order\/3ackwtoyn7oks4416fomn"} 

幫助我走出這個 我在JSP的工作。 預先感謝您。

+0

是JSON –

+1

響應東陽它的語法看起來像一個JSON –

+0

我不知道這是什麼,我需要訪問此。是的,它看起來像JSON。我也是初學者。 –

回答

1

因爲這看起來像一個簡單的web服務的答案(而不是完整的HTML頁面),我會使用Ajax發送表單和管理響應。

與jQuery,這是很容易使用$.ajax

$.ajax({ 
    url: //the URL 
    data: //the form value 
    method: //GET/POST 
    success: function(response){ 
     //decode the JSON response... 
     var url = $.parseJSON(response).paymentLink; 
     //then redirect/not sure since this could be cross-domain... 
     window.loacation = url; 
    }, 
    error: function(error){ 
     ... 
    } 
}) 

的唯一想到的是,形式不應該與提交輸入來送,你需要一個按鈕鏈接到這樣做Ajax調用的函數。

這可以在不JQuery的做,但我可以從內存中寫入這一點;)


如果你可以編輯JSP創建響應,您可以生成一個HTML直接返回值。

<html> 
<head> 
    <script> 
     window.location.href = '${paymentLink}'; 
    </script> 
</head> 
<body> 
    Redirection ... 
    <br>If nothing happend, you can <a href='${paymentLink}'>click here</a> to be redirected. 
</body> 
</html> 

${paymentLink}是EL,將打印這個變量的值(以及它的服務器上的名稱),以完成與URL的腳本。

一旦由客戶機接收到的,它就會被執行。

當然,這不會是在每一個瀏覽器跨域。如果不是這樣,您需要提供鏈接給用戶使用<a href='${paymentLink}'>Redirection</a> itsefl。

+0

我們可以用另一種方式做到這一點bcz我不知道阿賈克斯。 –

+0

@PriyankaGoyal由於服務器只返回一個JSON,你需要在某處管理它,**如果你可以更新JSP來創建響應,那麼你可以**。但所有你需要知道的關於ajax的是,這是一個異步請求者,而不是簡單的重新加載頁面,它在後臺發送請求並讓你管理響應。在這裏,由於您收到的只是一個文本(以JSON的形式),因此您需要像這樣管理它(我不知道其他解決方案來管理JSON響應)。 – AxelH

+0

@PriyankaGoyal,如果您可以更新生成響應的JSP,我已經添加了一個解決方案。給我一個這方面的反饋;) – AxelH

0

我認爲你正在尋找的響應消息,並重定向到某個地方 如果這樣你就可以使用下面的代碼

if(condition) 
{ 
response.sendRedirect("urpage.jsp"); 
} 

if(condition) 
    { 
    request.getRequestDispacher("page.jsp");//enter the name of page you want to redirect inside "" 
} 
+0

很長一段時間沒有寫Servlet,它是跨域嗎? – AxelH

+0

這是不同的東西 –

1

嘗試......

而提交表單寫入一個JS函數並獲取URL值。

 <head> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
      <script type="text/javascript"> 
      function check(){ 

//here you have to get value using element name or id (val1,val2,val3) 
       $.ajax({ 
        type:'GET', 
        data: {customerName: val1, customerEmail: val2,customerPhone: val3}, 
        url:'https://test.test.com/billpay/order/3ackwtoyn7oks4416fomn',// Replace with Your Exact URL 
        success: function(response){ 
         alert(response); 
         var json = JSON.parse(response); 
         var values = json.values; 

         for(var i in values) 
         { 
          var New_redirect = values[i].address; 
          alert(values[i].address); 

         } 

         window.loacation=New_redirect; 
        } 
       }); 
      }) 

} 
      </script> 


      </head> 
+0

評論是不適合擴展討論;這個談話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/135451/discussion-on-answer-by-karthick-anbazhagan-how-can-i-get-response-coming-from-from- W)。 –