大佬教程收集整理的这篇文章主要介绍了jQuery.getJSON调用ASP.NET方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
$(document).ready(function () { $.getJSON('Default2.aspx/GetPerson',{ 'firstname': 'brian','lastname': 'lee' },function (responsE) { alert(response.AgE); }); });
Default2.aspx代码:
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static String GetPerson(String firstname,String lastName) { Person p = new Person(firstname,lastName); return "{\"Age\":\"12\"}"; }
问题是 :
为什么不从我的脚本调用GetPerson方法?我在GetPerson中附加调试器,但似乎没有调用.
任何帮助将不胜感激!
$.ajax({ type: 'POST',url: 'Default2.aspx/GetPerson',dataType: 'json',// ... });
并且,请求格式也应该是JSON以匹配ResponseFormat:
// ... data: JSON.Stringify({ 'firstname': 'brian','lastname': 'lee' }),contentType: 'application/json'
或者,可以将ScriptMethod配置为使用GET:
[ScriptMethod(ResponseFormat = ResponseFormat.Json,UsehttpGet = truE)]
虽然,仍然需要为它设置contentType,所以不能使用$.getJSON():
$.ajax({ type: 'GET',contentType: 'application/json',数据将进行URL编码,但在此之前,每个值都需要进行JSON编码:
// ... data: { firstname: JSON.Stringify('brian'),lastname: JSON.Stringify('lee') }另请注意,ScriptMethods会将其响应包装在{“d”:…}对象中.并且,由于返回值是一个String,“d”的值是同一个未解析的String:
// ... success: function (responsE) { response = JSON.parse(response.d); alert(response.AgE); }
以上是大佬教程为你收集整理的jQuery.getJSON调用ASP.NET方法全部内容,希望文章能够帮你解决jQuery.getJSON调用ASP.NET方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。