前端的东西
<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);
}
}