2017-02-09 92 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)。 –