jQuery Ajax Demo

1. Htmlajax

  $(document).ready(function(){
    $("#bt").click(function(){
      $.ajax({
        type: "post",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        url: "SvJson",
        data: {some:"some 文本"},
        beforeSend: function(XMLHttpRequest){alert("before Send.");},
        complete: function(){alert("complete")},
        success: function(data){ alert("success"); alert("Some Thing: " + data.text);},
        error: function(){alert("error");}
      });
    });
  });json

  設置contentType爲防止中文上傳亂碼狀況。app

  dataType 設置返回數據類型。post

2. Javaurl

  String text = request.getParameter("some");code

  request.setCharacterEncoding("utf-8");
  response.setContentType("text/json; charset=utf-8");

  PrintWriter writer = response.getWriter();
  writer.write("{\"text\":\"success 終於\"}");
  writer.flush();
  writer.close();orm

  符合json格式的字符串便可,不必定要是json對象。對象

  PrintWriter 字符流需關閉。utf-8

相關文章
相關標籤/搜索