简体   繁体   English

如何在调用它的portlet中嵌入Web服务响应?

[英]How to embed web-service reponse within the portlet that called it?

Apologies for starting another thread but I kind of solved the issue of my first thread but now I run into a different issue. 抱歉,启动另一个线程,但是我已经解决了第一个线程的问题,但是现在遇到了另一个问题。

Background: 背景:

I have a portlet which takes 3 Parameters (Temperature,FromUnit,ToUnit) and passes them on to an external WebService located here: http://www.webservicex.net/ConvertTemperature.asmx/ConvertTemp 我有一个portlet,它需要3个参数(Temperature,FromUnit,ToUnit),并将它们传递给位于此处的外部WebService: http : //www.webservicex.net/ConvertTemperature.asmx/ConvertTemp

I did not want the portlet to actually redirect to the URL of the webService and the only way to do that appeared to be AJAX using jquery which I have done now. 我不希望portlet实际重定向到webService的URL,唯一的实现方法似乎是使用jquery进行的AJAX,我现在已经完成了。 However I also want the response of the webService to be embedded in the same portlet that I used to call it and that's where I am having issues. 但是,我还希望将webService的响应嵌入到我用来调用它的同一portlet中,这就是我遇到问题的地方。

This is what I got so far, here is my portlet page: 这是到目前为止,这是我的portlet页面:

  <html>
    <head>
        <meta charset="utf-8" />
        <title>Demo</title>
    </head>
    <body>


        <script src="http://localhost:8080/my-greeting-portlet/jquery.js"></script>
        <script type="text/javascript" src="http://localhost:8080/my-greeting-portlet/js/script.js"></script>



    <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>



    <%@ page import="javax.portlet.PortletPreferences" %>

    <portlet:defineObjects />
    <%

    PortletPreferences prefs = renderRequest.getPreferences();
    String Temperature = (String)prefs.getValue("Temperature","Temperature");

    PortletPreferences prefs2 = renderRequest.getPreferences();
    String FromUnit = (String)prefs2.getValue("FromUnit", "FromUnit");

    PortletPreferences prefs3 = renderRequest.getPreferences();
    String ToUnit = (String)prefs3.getValue("ToUnit","ToUnit");



    %>



    <portlet:renderURL var="editGreetingURL">

        <portlet:param name="jspPage" value="/edit.jsp" />

    </portlet:renderURL>


    <div id="contact_form">  
    <form name="callWebService" id="callWebService" action="">
     <fieldset> 
        <label for="Temperature" id="Temperature_label">Temperature </label>  
        <input type="text" name="Temperature" id="Temperature" size="30" value="" class="text-input" />  
        <label class="error" for="Temperature" id="Temperature_error">This field is required.</label>
        <br />  

        <label for="FromUnit" id="FromUnit_label">From unit  </label>  
        <input type="text" name="FromUnit" id="FromUnit" size="30" value="" class="text-input" />  
        <label class="error" for="FromUnit" id="FromUnit_error">This field is required.</label>
        <br />  

        <label for="ToUnit" id="ToUnit_label">To Unit    </label>  
        <input type="text" name="ToUnit" id="ToUnit" size="30" value="" class="text-input" />  
        <label class="error" for="ToUnit" id="ToUnit_error">This field is required.</label>  

        <br />  
        <input type="submit" name="submit" class="button" id="submit_btn" value="submit" />  
      </fieldset>  
    </form>  
    </div>  


    </body>
    </html>

And here is the jquery code: 这是jQuery代码:

$(function() {  
  $('.error').hide();  
  $(".button").click(function() {  
    // validate and process form here  
    var dataString = $("#callWebService").serialize();  
    // alert (dataString);return false;  
    $.ajax({  
      type: "POST",  
      url: "http://www.webservicex.net/ConvertTemperature.asmx/ConvertTemp",  
      data: $("#callWebService").serialize(), 
      success: function() {  
        $('#contact_form').html("<div id='message'></div>");  
        $('#message').html("<h2>Contact Form Submitted!</h2>")  
        .append("<p>We will be in touch soon.</p>")  
        .hide()  
        .fadeIn(1500, function() {  
          $('#message').append("<img id='checkmark' src='images/check.png' />");  
        });  
      }  
    });  
    return false;   



    $('.error').hide();  
      var Temperature = $("#Temperature").val();  
        if (Temperature == "") {  
      $("#Temperature_error").show();  
      $("#Temperature").focus();  
      return false;  
    }  
        var FromUnit = $("input#FromUnit").val();  
        if (FromUnit == "") {  
      $("label#FromUnit_error").show();  
      $("input#FromUnit").focus();  
      return false;  
    }  
        var ToUnit = $("input#ToUnit").val();  
        if (ToUnit == "") {  
      $("label#ToUnit_error").show();  
      $("input#ToUnit").focus();  
      return false;  
    }  

  });  
}); 

Everything seems to be working, or at least I do not get errors but it seems that this part of the code is completely ignored: 一切似乎都正常,或者至少我没有收到错误,但是似乎这部分代码被完全忽略了:

success: function() {  
           $('#contact_form').html("<div id='message'></div>");  
          $('#message').html("<h2>Contact Form Submitted!</h2>")  
        .append("<p>We will be in touch soon.</p>")  
          .hide()  
          .fadeIn(1500, function() {  
           $('#message').append("<img id='checkmark' src='images/check.png' />");  
      });  

When I press the "submit" button nothing happens. 当我按下“提交”按钮时,没有任何反应。 No redirection to the webservice URL (good) but also the custom message defined above does not show up (bad). 没有重定向到Web服务URL(良好),但是上面定义的自定义消息也没有显示(不良)。 The screen remains exactly as it is. 屏幕保持原样。

When I uncomment the "alert" in the jquery code and the parameters are definitely picked up correctly and I would assume that they are being passed to the webService URL but nothing else is happening. 当我取消注释jquery代码中的“警报”并且绝对正确地选择了参数时,我将假定它们已传递到webService URL,但没有其他反应。

Is this because the webservice URL returns a response that overwrites my message or something like that? 这是因为Web服务URL返回的响应会覆盖我的消息或类似内容吗?

How can I get the webService response embedded into the portlet? 如何将WebService响应嵌入到Portlet中?

Again, many thanks for looking at this, it is much appreciated! 再次感谢您对此的感谢,不胜感激!

You ran into a Cross Domain Scripting problem. 您遇到了跨域脚本问题。

Read this and this to resolve the problem 阅读对解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM