2014-03-05 20 views
1

我很忙於使用jPlayer完全基於jQuery的新項目。 我在jQuery中很新,所以我面臨很多問題,但現在我對jQuery感到很舒服。當我使用ajax調用jsp時,jsp返回大量額外的內容,特別是所有html標記

我的要求是訪問絕對url,如果給定的網址是相對的,因爲我使用了一些java代碼。每一件事情都運行良好,但爲了獲取我使用java代碼的絕對url,我使用了jsp頁面並使用ajax調用執行該代碼。問題是從jsp返回的值有很多額外的數據,通常是所有的html標籤。我看到這個問題已經被一些人問到,並且回覆

「使用servlet代替jsp,因爲jsp用於演示,這會輸出一些html」。 和我的代碼是:

function funAJAX(songURL){ 
    var path=document.getElementById("url").value, 
     ext=songURL.split('.').pop(), // trying to pull extension of link 
     xmlhttp, absUrl; 

    //alert(path); 

    if(window.XMLHttpRequest) { 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else { 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    // code for IE6, IE5 
    xmlhttp.onreadystatechange=function(){ 
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 

      // document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
      //$("#tempUrl").html(xmlhttp.responseText); 
      //storing val in div of id="tempUrl" 
      // absUrl=$("#tempUrl").find("info").text(); 
      //fetching the link only 

      var v=xmlhttp.responseText; 
      alert("value from ajax: " + v); 

      if(ext == "mp3" || ext == "Mp3") { // if absolute url then songUrl val will not change 
       // alert("extension: "+ext); 
       ext=ext; 
      } 
      else { // if relative link then storing the val returned from ajax as absolute link 
       // alert("in else part");        
       songURL=absUrl; 
      } 

      alert("i2: song url: " + songURL); // this will execute after returning val from jsp because of ajax call 
     } 
    }; 

    // alert("2: song url: "+songURL); //this will execute before sending req to jsp page 

    xmlhttp.open("GET", "" + path + "/html/player/songURLManipulation.jsp?songURL=" + songURL, true); //calling songURLManipulation.jsp with argument songUrl 

    xmlhttp.send(); //sending the req 
} 

以上是我的jsp頁面,擁有大批標籤 的「/> 這Liferay的問題,但主要的是要實現jplayer 和我的另一個JSP是一個我通過Ajax調用是

<% 
    String songURL=request.getParameter("songURL"); 

    String location=null; 

    if(songURL!=null) { 
     HttpURLConnection con = (HttpURLConnection)(new URL(songURL).openConnection()); 

     con.setInstanceFollowRedirects(false); 
     con.connect(); 

     location = con.getHeaderField("Location"); 

     response.getWriter().print(location); 

     out1.print(location); 
    } 

    //return location; 

    System.out.println("from song manipulation.jsp: "+songURL); 
%> 

我實現這一個通過的serveResource法,並呼籲通過AJAX該方法通過使用JSON對象,但我的問題是,它真的不是通過JSP頁面返回值實現的,如果我打電話通過上面描述的方式。 它是 真的很好成爲這個論壇的一部分。

回答

1

我的Ajax代碼

function funAJAXServlet(songURL,servletURL) 
{ 
    alert("from ajax fun"); 
    jQuery.ajax({ 
      url :servletURL,    
      data: {"songURL":songURL}, 
      type: "GET", 
      dataType: "json",                   //describe the type of value returned from serveResource class 
     success: function(data) {    
                               //to put data data into div part of html and data is coming from serveResource class. 
      var jsonobj=data.songUrlServlet; 
      alert("from servlet ajax: "+jsonobj);    
    } 
    }); 

} 

和我的serveResource方法

public void serveResource(ResourceRequest request, ResourceResponse response) 

    { 
     String songURL=request.getParameter("songURL"); 
     JSONObject jsobj=null; 
     String location=null; 
     HttpURLConnection con; 
     System.out.println("songURL from serveResourceUrl servlet method: "+songURL); 
     if(songURL!=null) 
     { 

       try 
       { 
       con = (HttpURLConnection)(new URL(songURL).openConnection()); 
       con.setInstanceFollowRedirects(false); 
       con.connect(); 
       location = con.getHeaderField("Location"); 
       System.out.println("$$$$$$$$$$$4 location val: "+location); 
       jsobj = JSONFactoryUtil.getJSONFactory().createJSONObject(); 
       if(location!=null) 
        jsobj.put("songUrlServlet",location); 
       else 
        jsobj.put("songUrlServlet","null"); 
       PrintWriter writer = response.getWriter();           //writing to the client page 
       writer.write(jsobj.toString());    
       } 
       catch (MalformedURLException e) 
       { 
        e.printStackTrace(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
     } 
     //return location; 
     else 
     System.out.println("from song serveResource method: "+songURL); 
    } 

這是工作的罰款。