當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
使用jQuery发送POST,Ajax请求返回JSON格式数据
生活随笔
收集整理的這篇文章主要介紹了
使用jQuery发送POST,Ajax请求返回JSON格式数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題:使用jQuery POST提交數據到PHP文件, PHP返回的json_encode后的數組數據,但jQuery接收到的數據不能解析為JSON對象,而是字符串{"code":-1,"msg":"123","data":[]}
jQuery get() 和 post() 方法用于通過 HTTP GET 或 POST 請求從服務器請求數據。
jQuery $.get() 方法$.get() 方法通過 HTTP GET 請求從服務器上請求數據,在URL地址上可以看到傳遞的參數,一般用于傳遞少量數據。
語法: $.get(URL,callback);詳細語法:$(selector).get(url, data, success(response,status,xhr),dataType)
jQuery $.post() 方法$.post() 方法通過 HTTP POST 請求從服務器上請求數據,在URL地址上不可以看到傳遞的參數,一般用于傳遞大量數據。
語法:$.post(URL,data,callback);詳細語法: jQuery.post(url,data,success(data, textStatus,jqXHR),dataType)
查看$.post()詳細的語法:jQuery.post(url,data,success(data, textStatus,jqXHR),dataType)你會發現,最后邊有個參數 dataType,這個就是問題所在。這個dataType是可選參數,它規定預期的服務器響應的數據類型。默認執行智能判斷(xml、json、script 或html)。
詳細說明該函數是簡寫的 Ajax 函數,等價于:$.ajax({? type: 'POST',? url: url,? data: data,? success: success,? dataType: dataType});
解決:
$.post() 加上最后邊的一個可選參數 dataType 為“json”類型
示例:獲得 test.php 頁面返回的 json 格式的內容:$.post("test.php", { "func": "getNameAndTime" },? ?function(data){? ??alert(data.name); // John? ??console.log(data.time); //?2pm? ?},"json");
示例:獲得 test.php 頁面的內容,并存儲為 XMLHttpResponse 對象,并通過 process() 這個JavaScript 函數進行處理:$.post("test.php", { name: "John", time: "2pm" },? ?function(data){? ??process(data);? ?},"xml");
Struts2中對于后臺向前端返回JSON格式數據一般使用以下方式:
<span style="font-size:18px;"> public void writeJson2Resp(String json) throws IOException {HttpServletResponse resp = ServletActionContext.getResponse();resp.setCharacterEncoding("gbk");resp.setContentType("text/html;charset=gbk"); //這一句沒加入會導致亂碼PrintWriter out = resp.getWriter();out.write(json);out.flush();out.close();}</span>
在 BaseAction 實現該方法,那么其他的 Action? 只要繼承了改類,就可以使用該方法向前臺頁面返回JSON格式數據
參考鏈接:jQuery ajax 參考手冊http://www.w3school.com.cn/jquery/jquery_ref_ajax.asp
HTTP 方法:GET 對比 POSThttp://www.w3school.com.cn/tags/html_ref_httpmethods.asp
jQuery ajax - post() 方法:http://www.w3school.com.cn/jquery/ajax_post.asp
jQuery ajax - get() 方法http://www.w3school.com.cn/jquery/ajax_get.asp
jQuery get() 和 post() 方法用于通過 HTTP GET 或 POST 請求從服務器請求數據。
jQuery $.get() 方法$.get() 方法通過 HTTP GET 請求從服務器上請求數據,在URL地址上可以看到傳遞的參數,一般用于傳遞少量數據。
語法: $.get(URL,callback);詳細語法:$(selector).get(url, data, success(response,status,xhr),dataType)
jQuery $.post() 方法$.post() 方法通過 HTTP POST 請求從服務器上請求數據,在URL地址上不可以看到傳遞的參數,一般用于傳遞大量數據。
語法:$.post(URL,data,callback);詳細語法: jQuery.post(url,data,success(data, textStatus,jqXHR),dataType)
查看$.post()詳細的語法:jQuery.post(url,data,success(data, textStatus,jqXHR),dataType)你會發現,最后邊有個參數 dataType,這個就是問題所在。這個dataType是可選參數,它規定預期的服務器響應的數據類型。默認執行智能判斷(xml、json、script 或html)。
詳細說明該函數是簡寫的 Ajax 函數,等價于:$.ajax({? type: 'POST',? url: url,? data: data,? success: success,? dataType: dataType});
解決:
$.post() 加上最后邊的一個可選參數 dataType 為“json”類型
示例:獲得 test.php 頁面返回的 json 格式的內容:$.post("test.php", { "func": "getNameAndTime" },? ?function(data){? ??alert(data.name); // John? ??console.log(data.time); //?2pm? ?},"json");
示例:獲得 test.php 頁面的內容,并存儲為 XMLHttpResponse 對象,并通過 process() 這個JavaScript 函數進行處理:$.post("test.php", { name: "John", time: "2pm" },? ?function(data){? ??process(data);? ?},"xml");
Struts2中對于后臺向前端返回JSON格式數據一般使用以下方式:
<span style="font-size:18px;"> public void writeJson2Resp(String json) throws IOException {HttpServletResponse resp = ServletActionContext.getResponse();resp.setCharacterEncoding("gbk");resp.setContentType("text/html;charset=gbk"); //這一句沒加入會導致亂碼PrintWriter out = resp.getWriter();out.write(json);out.flush();out.close();}</span>
在 BaseAction 實現該方法,那么其他的 Action? 只要繼承了改類,就可以使用該方法向前臺頁面返回JSON格式數據
參考鏈接:jQuery ajax 參考手冊http://www.w3school.com.cn/jquery/jquery_ref_ajax.asp
HTTP 方法:GET 對比 POSThttp://www.w3school.com.cn/tags/html_ref_httpmethods.asp
jQuery ajax - post() 方法:http://www.w3school.com.cn/jquery/ajax_post.asp
jQuery ajax - get() 方法http://www.w3school.com.cn/jquery/ajax_get.asp
關注公眾號,分享干貨,討論技術
轉載于:https://www.cnblogs.com/molashaonian/p/9097617.html
總結
以上是生活随笔為你收集整理的使用jQuery发送POST,Ajax请求返回JSON格式数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [开源]FreeSCADA的通道数据与控
- 下一篇: Spring MVC 中 Handler