2015-02-07 61 views
2

我用下面的代码来发送一些信息给我的servlet处理数据:通过在角JS AJAX调用发送阵列

$http({ 
       method: "GET", 
       url: "http://localhost:8080/purchase/AddInfo", 
       data: { 
        addArray : "sample" 
       } 
      }) 
       .success(function (data, status, headers, config) { 
        typesHash.push({id:data.id,name : data.name, price : data.price,unit:2.5 }); 
       }) 
       .error(function (data, status, headers, config) { 

       }); 

和它完美; 但你可以看到我想要发送的参数作为数组不是字符串,可以说我有一个数组如下:

var typesHash=[ 
       {id:'1', name : 'lemon', price : 100,unit:2.5 },  
       {id:'2', name : 'meat', price : 200,unit:3.3 }]; 

现在我想这个数组发送到服务器,一个快速的和丑陋方式是循环通过我有的数组,并发送信息作为字符串,但我相信应该有更好的方法,任何人都可以帮助吗?

更新:因为它是建议我改变了我的代码如下:

$http({ 
       method: "post", 
       url: "http://localhost:8080/purchase/AddInfo", 
        addArray : typesHash 

      }) 
       .success(function (data, status, headers, config) { 
        typesHash.push({id:data.id,name : data.name, price : data.price,unit:2.5 }); 
       }) 
       .error(function (data, status, headers, config) { 

       }); 

,但我得到空当我尝试rceive它,这是我收到它在我的servlet:

String arr= request.getParameter("addArray"); 
    System.out.println(arr); 

更新2:这是最更新的代码

我的servlet:

protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 

    String actionType = request.getParameter("addArray"); 
    System.out.println(actionType); 
    PrintWriter out = response.getWriter(); 
    response.setContentType("text/html"); 
    String str = "{ \"id\": \"1\",\"name\": \"ali\",\"price\": \"100000\"}"; 
    // System.out.println(str); 
    out.println(str); 
} 

/** 
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
*  response) 
*/ 
protected void doPost(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    doGet(request, response); 

} 

我的JS:

$http({ 
       method: "post", 
       url: "http://localhost:8080/purchase/AddInfo", 
       data: { addArray : typesHash } 


      }) 
       .success(function (data, status, headers, config) { 
        typesHash.push({id:data.id,name : data.name, price : data.price,unit:2.5 }); 
       }) 
       .error(function (data, status, headers, config) { 

       }); 
+0

嗯,我明白了!我认为这是你需要的https://docs.angularjs.org/api/ng/service/$http – 2015-02-07 22:52:33

+0

嘿这条线的输出是什么“System.out.println(arr);”在servlet控制台中?并且不要使用get post,因为在java中获得大小限制。 – squiroid 2015-02-08 06:03:09

+0

还有一件事这一行addArray:typesHash,应该是数据:typesHash你的建议system.println – squiroid 2015-02-08 06:05:11

回答

1

嘿,你可以做这样的: -

$http({ 
     method: 'POST', 
     headers: {'Content-Type': 'application/json'}, 
     url: "http://localhost:8080/purchase/AddInfo", 
     data: { addArray : typesHash } 


     }) 
      .success(function (data, status, headers, config) { 
       typesHash.push({id:data.id,name : data.name, price : data.price,unit:2.5 }); 
      }) 
      .error(function (data, status, headers, config) { 

      }); 

来源: - http://www.doublecloud.org/2013/09/angular-javascript-framework-interacting-with-java-servlet-backend/

+0

非常感谢我从你这里学到了很多,只是一个简短的问题,而我去问一个问题,你现在解决了问题,现在还有另一个问题,你可以请看一下吗?http:// stackoverflow。 COM /问题/ 28397305 /时 - 我 - 把-NG-控制器在-DIV-INSTEAD-OF-体自动完成 - 停止 - 工作 – 2015-02-08 17:57:57

0

HTTP有几种方法吧: 'GET' - 允许你接受来自请求的服务器数据。 'POST' - 允许您发送数据到服务器并接受。 '把' - 更新数据 等等

试试这个代码:

$http({ 
      method: "post", 
      url: "http://localhost:8080/purchase/AddInfo", 
       {addArray: typesHash} 

     }) 
      .success(function (data, status, headers, config) { 
       typesHash.push({id:data.id,name : data.name, price : data.price,unit:2.5 }); 
      }) 
      .error(function (data, status, headers, config) { 

      }); 
+0

谢谢,但是当我尝试进入我的servlet我不能...我怎样才能得到它在servlet? – 2015-02-07 22:55:01

+0

服务器返回您的错误是什么?也许你需要为一个post请求定义一个特定的服务器端代码?我不太熟悉基于Java的服务器,因为它是一个非常古老而且效率不高的技术。 – 2015-02-07 22:58:16

+0

我通常会得到如下的参数:String actionType = request.getParameter(“addArray”);并在JavaScript中我有addArray:typeHash,但我不知道如何在这种情况下gte它 – 2015-02-07 23:02:48