7

我有一个数组,我想在我的服务器上获取其内容。我一直在浏览互联网页面,试图找到如何做到这一点,但尚未成功。

假设我有一个服务器,我希望我的 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>
4

3 回答 3

13

那是一个数组,不需要对其进行字符串化,jQuery 会将数据转换为您的有效查询字符串

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

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

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

$array = $_POST['myArray'];
于 2012-11-05T16:24:46.693 回答
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);
        }

    }
于 2012-11-05T16:44:52.743 回答
1

jQuery 将为您对数组进行字符串化,因此您在示例中有效地对其进行了两次编码,这导致了您的问题。试试这个:

var arr = ["one","two","three"];
$.ajax({
    url: "http://url_name_here.com",
    type: "POST",
    data: { myArray : arr }
});
于 2012-11-05T16:25:57.693 回答