2013-08-17 37 views
1

我在嘗試獲取跨域ajax請求時遇到問題,儘管我在Stack Overflow上找到了許多解決方案,但無法使其工作。我無法使用Weblogic/Spring進行跨域ajax 3

$.ajax({ 
     url : 'http://SERVER:PORT/CONTEXT/RESOURCE.html?someParameter=1234', 
     dataType : 'json', 
     success: function(xhr) { 
      alert('ok '+JSON.stringify(xhr)); 
     }, 
       error : function(xhr) { 
         alert('error '+JSON.stringify(xhr)); 
       } 
    }); 

這樣做只是一個標準的$就呼叫與數據類型「json的」服務器一個空白響應和狀態文本「錯誤」響應,就像這樣:

error {"readyState":0,"responseText":"","status":0,"statusText":"error"} 

所以,我想簡單地改變數據類型爲「JSONP」的建議在其他線程,但這次還是轉到錯誤有以下反應條件:

error {"readyState":4,"status":200,"statusText":"success"} 

和「parsererror」的錯誤消息 燁沒有數據。

什麼給?

我是否需要在服務器端做一些特殊的事情,因爲它是Weblogic中的Spring MVC?

編輯: jQuery的版本1.9.1 Spring的MVC 3

EDIT2:哦,是的,我也試過$ .getJSON但該命令似乎什麼都不做 - 當我運行的代碼替換$ $阿賈克斯.getJSON沒有任何反應。沒有反應,我沒有看到控制檯中發生任何錯誤,並且沒有看到網絡請求轉到該URL。我也改變了第二次嘗試的語法,我把它叫做$ .getJSON(url,callback);但這並沒有改變任何東西

編輯3:我還應該提到當我使用「json」數據類型運行原始代碼並查看Firebug的響應選項卡時,它是空的。但是當我使用「jsonp」運行第二個代碼時,我在「響應」選項卡中看到了JSON文本。所以很奇怪爲什麼它仍然會拋出一個錯誤。

回答

1

好的,經過更多的研究,我終於找到了原因 - 是的,我確實需要在服務器端做一些事情來支持jsonp。我最終編寫了一個servlet過濾器,它將返回的json字符串封裝在適當的回調函數中。

每天學習新東西!

public class JsonPCallbackFilter extends OncePerRequestFilter { 

    Logger logger = Logger.getLogger(JsonPCallbackFilter.class); 

    @Override 
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) 
      throws ServletException, IOException { 

     //logger.debug("Filter: "+request.getRequestURI()); 

     @SuppressWarnings("unchecked") 
     Map<String, String[]> parms = request.getParameterMap(); 

     if(parms.containsKey("callback")) { 
      logger.debug("Wrapping response with JSONP callback '" + parms.get("callback")[0] + "'"); 

      OutputStream out = response.getOutputStream(); 

      ByteResponseWrapper wrapper = new ByteResponseWrapper(response); 

      chain.doFilter(request, wrapper); 

      StringBuffer sb = new StringBuffer(); 
      sb.append(parms.get("callback")[0] + "("); 
      sb.append(new String(wrapper.getBytes())); 
      sb.append(new String(");")); 

      out.write(sb.toString().getBytes()); 

      wrapper.setContentType("text/javascript;charset=UTF-8"); 
      response.setContentLength(sb.length()); 

      out.close(); 
     } else { 
      chain.doFilter(request, response); 
     } 
    } 
} 

static class ByteOutputStream extends ServletOutputStream { 
    private ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    @Override 
    public void write(int b) throws IOException { 
     bos.write(b);    
    } 

    public byte[] getBytes() { 
     return bos.toByteArray(); 
    } 
} 

static class ByteResponseWrapper extends HttpServletResponseWrapper { 
    private PrintWriter writer; 
    private ByteOutputStream output; 

    public byte[] getBytes() { 
     writer.flush(); 
     return output.getBytes(); 
    } 

    public ByteResponseWrapper(HttpServletResponse response) { 
     super(response); 
     output = new ByteOutputStream(); 
     writer = new PrintWriter(output); 
    } 

@Override 
public PrintWriter getWriter() { 
    return writer; 
} 

@Override 
public ServletOutputStream getOutputStream() throws IOException { 
    return output; 
    } 
} 

    <filter> 
     <filter-name>jsonpFilter</filter-name> 
     <filter-class>com.blahblah.JsonPCallbackFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>jsonpFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
+0

太好了!所有這一切都沒有。發現當我這樣做時,我無法獲得會話對象,請猜測它的跨域cookie限制。回到繪圖板。 – Trant