在角JS Ajax调用发送阵列阵列、JS、Ajax

2023-09-11 01:24:10 作者:热情总会变淡

我用下面的code发送一些信息给我的servlet处理数据:

  $ HTTP({
                方法:GET,
                网址:HTTP://本地主机:8080 /购买/ ADDINFO
                数据: {
                    addArray:样品
                }
            })
                .success(功能(数据,状态,头,配置){
                     typesHash.push({ID:data.id,名称:data.name,价格:data.price,单位:2.5});
                })
                .error(功能(数据,状态,头,配置){

                });
 

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

  VAR typesHash = [
                 {ID:1,名称:柠檬,价格:100,单位:2.5},
                 {编号:'2',名称:'肉',价格:200,单位:3.3}]。
 

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

更新:因为它是建议我改变了我的code以下内容:

  $ HTTP({
                方法:后,
                网址:HTTP://本地主机:8080 /购买/ ADDINFO
                    addArray:typesHash

            })
                .success(功能(数据,状态,头,配置){
                     typesHash.push({ID:data.id,名称:data.name,价格:data.price,单位:2.5});
                })
                .error(功能(数据,状态,头,配置){

                });
 
JavaScript Ajax

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

 字符串ARR =的request.getParameter(addArray);
    的System.out.println(ARR);
 

更新2:这是最新的code 的

我的servlet:

 保护无效的doGet(HttpServletRequest的要求,
        HttpServletResponse的响应)抛出了ServletException,IOException异常{
    // TODO自动生成方法存根

    串操作类型的request.getParameter =(addArray);
    的System.out.println(操作类型);
    PrintWriter的输出= response.getWriter();
    response.setContentType(text / html的);
    字符串str ={\ID \:\1 \,\名字\:\阿里\,\价格\:\100000 \};
    //的System.out.println(STR);
    通过out.println(STR);
}

/ **
 * @see的HttpServlet#的doPost(HttpServletRequest的要求,HttpServletResponse的
 *      响应)
 * /
保护无效的doPost(HttpServletRequest的要求,
        HttpServletResponse的响应)抛出了ServletException,IOException异常{
    // TODO自动生成方法存根
    的doGet(请求,响应);

}
 

我的JS:

  $ HTTP({
                方法:后,
                网址:HTTP://本地主机:8080 /购买/ ADDINFO
               数据:{addArray:typesHash}


            })
                .success(功能(数据,状态,头,配置){
                     typesHash.push({ID:data.id,名称:data.name,价格:data.price,单位:2.5});
                })
                .error(功能(数据,状态,头,配置){

                });
 

解决方案

嘿,你能做到这一点是这样的: -

  $ HTTP({
      方法:POST,
      标题:{内容类型:应用/ JSON的},
      网址:HTTP://本地主机:8080 /购买/ ADDINFO
      数据:{addArray:typesHash}


        })
            .success(功能(数据,状态,头,配置){
                 typesHash.push({ID:data.id,名称:data.name,价格:data.price,单位:2.5});
            })
            .error(功能(数据,状态,头,配置){

            });
 

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

I use the following code to send some information to my servlet to process data:

 $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) { 

                });

and it works perfectly; but as you can see I want to send the parameters as an array not a string , lets say I have an array as follow:

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

now I want to send this array to the server, one quick and ugly way is to loop through the array that I have and send send the information as an string but I believe there should be a better way , can any one help?

Update: as it is suggested I changed my code to the following :

 $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) { 

                });

But I get null when I try to rceive it and this is how I receive it in my servlet:

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

Update 2:Here is the most updated code

My 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);

}

My 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) { 

                });

解决方案

Hey you can do it like this :-

 $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) { 

            });

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