【发布时间】:2014-08-10 12:48:31
【问题描述】:
我有一个网络应用程序。在里面我有一个 asmx 文件“MyWebServices.asmx”,其中我有一个将 json 对象发送到我的 WebForm2.aspx 的 Web 方法。我的问题是如何用 Javascript 捕获这个对象存储它并用 javascript 显示它。 我在 MyWebServices.asmx 上的代码:
public class apointment
{
public string Fname{ get; set; }
public string Lname{ get; set; }
public string customerid { get; set; }
}
[WebMethod]
public string myapointment()
{
apointment myapointment1= new apointment();
myapointment1.customerid = "123POW";
myapointment1.Fname = "John";
myapointment1.Lname = "JohnsLname";
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(myapointment1);
return sJSON;
}
我在 .net 页面 Javascript 上的代码:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: "services/MyWebServices.asmx/myapointment",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Insert the returned HTML into the <div>.
var myrant = data.d;
$('#RSSContent').html(data.d);
}
});
});
</script>
问题在于这段代码我正在使用一个字符串:
{"Fname":"John","Lname":"JohnsLname","customerid":"123POW"}
如何将此字符串转换为对象类型约会?我问是因为之后我可以在 html 上正确显示,我想创建约会列表。
【问题讨论】:
-
在
myapointment方法中返回apointment对象不是字符串... -
声明它以便它返回
apointment并用[ScriptMethod(ResponseFormat = ResponseFormat.Json)]标记你的方法 -
好的,我做到了。如何使用 javascript 访问它?
-
datainsuccess: function (data){是你的对象... -
我已将代码更改为 $('#RSSContent').html(data.Fname);我什么都没拿
标签: c# javascript asp.net json web-services