将javascript数组发送到服务器

时间:2012-11-05 16:22:03

标签: javascript jquery ajax

我有一个数组,我希望在我的服务器上获得内容。我一直在浏览网页试图找到如何做到这一点,但没有成功。

假设我有一台服务器,我想在我的javascript中使用这个数组进入我服务器上的文件,我该怎么做?

我一直在浏览网页,寻找如何做到这一点,我想出了以下代码:

<html>
    <!-- installs jquery and ajax. -->
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    <script>
        var arr = ["one","two","three"];
        arr = JSON.stringify(arr);

        $.ajax({
            url: "http://url_name_here.com",
            type: "POST",
            data: {
                myArray : arr
            }
        });

        alert('hello');
    </script>
</html>

3 个答案:

答案 0 :(得分:10)

这是一个数组,没有必要对其进行字符串化,jQuery会将数据转换为有效的查询字符串

var arr=["one","two","three"];

$.ajax({
    url: "/urltoMyOwnSite.php",
    type: "POST",
    data: {myArray : arr}
});

PHP(如果这就是你正在使用的那样)

$array = $_POST['myArray'];

答案 1 :(得分:2)

前端的东西

<html>
    <!-- installs jquery and ajax. -->
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    <script>
    $(function(){
     var arr = ["one","two","three"];
        arr = JSON.stringify(arr);

        $.ajax({
            url: "http://url_name_here.com",
            type: "POST",
            data: {
                myArray : arr
            }
        }).done(function(data,text,jQxhr){
       alert("success");
    });
});
    </script>
</html>

后端服务器(java b / c这是我在dev atm中打开的)

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import org.json.simple.JSONObject;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    import java.io.FileOutputStream;

    public class MyServlet extends HttpServlet
    {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException,
                IOException
        {
            JSONObject jsonObj = (JSONObject) org.json.simple.JSONValue.parse(request.getParameter("myArray"));
           // write out your file IO.
JSONObject jsonObj = (JSONObject) org.json.simple.JSONValue.parse(request.getParameter("myArray"));
        FileOutputStream file = new FileOutputStream(java.io.File.createTempFile("myArray-",Long.toString((new Date()).getTime())));
        FileChannel fc = file.getChannel();
        ByteBuffer sw = ByteBuffer.allocate(jsonObj.toJSONString().length());
        sw.clear();
        sw.put(jsonObj.toJSONString().getBytes());
        sw.flip();

        while(sw.hasRemaining()) {
            fc.write(sw);
        }
fc.close();
//because its much easier than a flat file, we can write it back in the server response.
        org.json.simple.JSONValue.writeJSONString(jsonObj, response.getWriter());
        }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException,
                IOException
        {
            doGet(request, response);
        }

    }

答案 2 :(得分:1)

jQuery会为你进行字符串化,所以你在你的例子中有效地对它进行了两次编码,这会导致你的问题。试试这个:

var arr = ["one","two","three"];
$.ajax({
    url: "http://url_name_here.com",
    type: "POST",
    data: { myArray : arr }
});