簡體   English   中英

如何在調用它的portlet中嵌入Web服務響應?

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

抱歉,啟動另一個線程,但是我已經解決了第一個線程的問題,但是現在遇到了另一個問題。

背景:

我有一個portlet,它需要3個參數(Temperature,FromUnit,ToUnit),並將它們傳遞給位於此處的外部WebService: http : //www.webservicex.net/ConvertTemperature.asmx/ConvertTemp

我不希望portlet實際重定向到webService的URL,唯一的實現方法似乎是使用jquery進行的AJAX,我現在已經完成了。 但是,我還希望將webService的響應嵌入到我用來調用它的同一portlet中,這就是我遇到問題的地方。

這是到目前為止,這是我的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>

這是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;  
    }  

  });  
}); 

一切似乎都正常,或者至少我沒有收到錯誤,但是似乎這部分代碼被完全忽略了:

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' />");  
      });  

當我按下“提交”按鈕時,沒有任何反應。 沒有重定向到Web服務URL(良好),但是上面定義的自定義消息也沒有顯示(不良)。 屏幕保持原樣。

當我取消注釋jquery代碼中的“警報”並且絕對正確地選擇了參數時,我將假定它們已傳遞到webService URL,但沒有其他反應。

這是因為Web服務URL返回的響應會覆蓋我的消息或類似內容嗎?

如何將WebService響應嵌入到Portlet中?

再次感謝您對此的感謝,不勝感激!

您遇到了跨域腳本問題。

閱讀對解決問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM