如何让Ajax读取我的json字符串 [英] How do I get Ajax to read my json string
本文介绍了如何让Ajax读取我的json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个子程序:
I have a subroutine:
[WebMethod]
public static string gvAjax()
{
List<Table1> gvlist = new List<Table1>();
...
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var json1 = serializer.Serialize(gvlist).ToString();
return json1;
}
我从表格
I get a string from the WebMethod of the form
"[{"Id":1,"col1":"11","col2":"22","col3":"33"},"{"Id":2,"col1":"44","col2":"55","col3":"66"},"{"Id":3,"col1":"77","col2":"88","col3":"99"}]"
通过将子例程发送到文本框,json字符串以字符串形式存在。
警报给出消息未定义。
如何让Ajax读取我的json字符串?
The json string exists in string form by sending the subroutine to a textbox.
The alert gives a message of "undefined".
How do I get Ajax to read my json string?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#<%= Button2.ClientID %>").click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/Default.aspx/gvAjax",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({}),
async: true,
cache: false,
success: function (data) {
alert(data.d);
...
},
error: function (x, e) {
alert("Error: " + x.responseText);
}
});
});
});
</script>
推荐答案
(function(){
(function () {
( #<%= Button2.ClientID%>)。click(function(e){
e.preventDefault();
("#<%= Button2.ClientID %>").click(function (e) { e.preventDefault();
.ajax({
type: POST,
url: / Default.aspx / gvAjax,
contentType: application / json; charset = utf-8,
dataType: json,
data:JSON.stringify({}) ,
async :< span class =code-keyword> true ,
cache: false ,
success:function(data){
alert(data.d);
...
},
错误:函数(x,e){
alert( 错误: + x.responseText);
}
});
});
});
< / script >
.ajax({ type: "POST", url: "/Default.aspx/gvAjax", contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify({}), async: true, cache: false, success: function (data) { alert(data.d); ... }, error: function (x, e) { alert("Error: " + x.responseText); } }); }); }); </script>
这篇关于如何让Ajax读取我的json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文