2015-08-03 80 views
0

我試圖JSON字符串以dB爲單位JSON值當Ajax請求發送返回null

繼發送到服務器,然後存儲它是我的代碼:

$.ajax({ 
     url: 'JsonProcessor.do', 
     type: 'post', 
     dataType: 'json', 
     data: { 
       loadProds: 1, 
       test: JSON.stringify(StateObject) 
       }, 
     success: function (data) 
     { 
     console.log("worked"); 
     }, 
     error: function (data) { 
     alert('failed'); 
     } 
     }); 

的Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     String json = request.getParameter("test"); 
     System.out.println("json is: " + json); 
     Connection con = null; 
     ResultSet rs = null; 
     try { 
      con = OracleDBConnection.getConnection(); 
      String query = "insert into JJS (JSON_INFO) values(?)"; 
      PreparedStatement pst = con.prepareStatement(query); 
      pst.setString(1, json); 
      rs = pst.executeQuery(); 
      if (rs.next()) { 
       System.out.println("sucess"); 
      } else { 
       System.out.println("failed"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("DB releated exception"); 
     } 

但是,當我嘗試打印request.getparameter(json)時,它返回null。

這是JSON我試圖發送:

{ 
    "cells": [ 
     { 
      "type": "basic.Rect", 
      "position": { 
       "x": -2, 
       "y": 33 
      }, 
      "size": { 
       "width": 71, 
       "height": 625 
      }, 
      "angle": 0, 
      "isInteractive": false, 
      "id": "e276b993-af5e-4e32-9879-fc3d817c0811", 
      "z": 1, 
      "attrs": { 
       "rect": { 
        "fill": "#EEEEEE", 
        "stroke": "#008B8B", 
        "stroke-width": 2 
       }, 
       ".": { 
        "magnet": false 
       } 
      } 
     }, 
     { 
      "type": "basic.Rect", 
      "position": { 
       "x": 10, 
       "y": 50 
      }, 
      "size": { 
       "width": 51, 
       "height": 41 
      }, 
      "angle": 0, 
      "isInteractive": false, 
      "id": "f8826904-678c-4857-ad46-f86e69d1db32", 
      "z": 2, 
      "attrs": { 
       "rect": { 
        "fill": "#D6F2FC", 
        "stroke": "#7E7E7E" 
       }, 
       ".": { 
        "magnet": false 
       } 
      } 
     }, 
     { 
      "type": "basic.Circle", 
      "size": { 
       "width": 53, 
       "height": 53 
      }, 
      "position": { 
       "x": 8, 
       "y": 130 
      }, 
      "angle": 0, 
      "isInteractive": false, 
      "id": "e4ee7b74-3c06-4e1e-8ce7-8baf6743c27c", 
      "z": 3, 
      "attrs": { 
       ".": { 
        "magnet": false 
       }, 
       "circle1": { 
        "fill": "white", 
        "stroke-width": 2, 
        "stroke": "green" 
       } 
      } 
     }, 
     { 
      "type": "basic.Circle", 
      "size": { 
       "width": 53, 
       "height": 53 
      }, 
      "position": { 
       "x": 8, 
       "y": 225 
      }, 
      "angle": 0, 
      "isInteractive": false, 
      "id": "04d4a40b-f99b-4c16-89a9-7d072e30c4ad", 
      "z": 4, 
      "attrs": { 
       ".": { 
        "magnet": false 
       }, 
       "circle2": { 
        "fill": "white", 
        "stroke": "green" 
       } 
      } 
     }, 
     { 
      "type": "basic.Circle", 
      "size": { 
       "width": 53, 
       "height": 53 
      }, 
      "position": { 
       "x": 8, 
       "y": 320 
      }, 
      "angle": 0, 
      "isInteractive": false, 
      "id": "97a01219-b7e2-4a52-9248-eb41dc7443b4", 
      "z": 5, 
      "attrs": { 
       ".": { 
        "magnet": false 
       }, 
       "circle3": { 
        "fill": "white", 
        "stroke": "green" 
       } 
      } 
     }, 
     { 
      "type": "basic.Rect", 
      "position": { 
       "x": 10, 
       "y": 420 
      }, 
      "size": { 
       "width": 51, 
       "height": 41 
      }, 
      "angle": 0, 
      "isInteractive": false, 
      "id": "ed74bfd7-c7e2-4f68-85c3-0f1865243d3e", 
      "z": 6, 
      "attrs": { 
       ".": { 
        "magnet": false 
       }, 
       "rectGroup0": { 
        "fill": "white", 
        "stroke": "#7E7E7E" 
       } 
      } 
     }, 
     { 
      "type": "basic.Rect", 
      "position": { 
       "x": 35, 
       "y": 505 
      }, 
      "size": { 
       "width": 45, 
       "height": 45 
      }, 
      "angle": 0, 
      "isInteractive": false, 
      "id": "008c90ea-2598-4761-8775-fc3601c8935d", 
      "z": 7, 
      "attrs": { 
       "rect": { 
        "fill": "#FFED6B", 
        "stroke": "#DBCB62", 
        "width": 1, 
        "height": 1, 
        "stroke-width": 1, 
        "transform": "rotate(45)" 
       }, 
       ".": { 
        "magnet": false 
       } 
      } 
     } 
    ] 
} 
+1

什麼是'request.getparament'? :o – Karthik

回答

4

有你的代碼中的多個問題:

發送時,jQuery的已經做了你的字符串化,所以你自己打電話時,你實際上是在創造中發送的JSON字符串屬性

$.ajax({ 
    url: 'JsonProcessor.do', 
    type: 'post', 
    dataType: 'json', 
    data: { 
      loadProds: 1, 
      test: JSON.stringify(StateObject) 
      }, 
     ... 

實際上會科瑞(您可以通過觀看使用瀏覽器發送的數據很容易地檢查了這一點) Ë看起來像一個HTTP請求正文:

{ 
    loadProds: 1, 
    test: "{\"state\": .... " 
} 

所以只寫:

$.ajax({ 
    url: 'JsonProcessor.do', 
    type: 'post', 
    dataType: 'json', 
    data: { 
      loadProds: 1, 
      test: StateObject 
      }, 
     ... 

,你將在這部分罰款。

至於服務器部分,實際上是在尋找名爲「test」的請求參數。但這不是正在發送的內容。相反,你會得到一個包含json的「body」。當使用dataType:json時,根本沒有標準的請求參數。

所以你的情況是

String json = IOUtils.toString(request.getInputStream()); 

而且不是說你檢索JSON有包括「loadProps:1」 ...... 然後,您可能希望將字符串轉換爲對象(即使用傑克遜),所以你可以使用它。

+0

什麼是loadProps:1?我從其他站點得到它 – kittu

+0

loadProps:我想知道它的主意,它只是額外的數據,如果你不使用它,只需寫入「data:StateObject」,那麼它只會保護你的數據 jackson:你需要apache commons lang cp(https://commons.apache.org/proper/commons-lang/) – Niko