我试图从c#webmethod获取jquery ajax函数中的json字符串数组。我收到错误500 [暂停] [英] I am trying to get the array of json string in jquery ajax function from c# webmethod . I am getting error 500 [on hold]

查看:60
本文介绍了我试图从c#webmethod获取jquery ajax函数中的json字符串数组。我收到错误500 [暂停]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在c#webmethod的aspx页面中的jquery ajax函数中获取json字符串数组。我在jquery警报中收到错误500.



Jquery代码:



I am trying to get the array of json string in jquery ajax function in aspx page from c# webmethod resides in code behind . I am getting error 500 in my jquery alert .

Jquery code:

$(document).ready(function () {
    $.ajax({
        type: 'GET',
        dataType: 'json',
        async: false,
        url: 'MyWebService.asmx/SayHelloJson',
        cache: false,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert('success');
        },
        error: function (err) {
            alert(err.status);
        }
    });
});

C# code:

<pre lang="cs">[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class MyWebService : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public string SayHelloJson()
    {
        var data = new { Greeting = &quot;Hello&quot;, Name = &quot;test&quot; };

       JavaScriptSerializer js=new JavaScriptSerializer();

        return js.Serialize(data);
    }
}</pre>

推荐答案

(文件).ready(function(){
(document).ready(function () {


.ajax({
type:'GET',
dataType:'json',
async:false,
url:'MyWebService .asmx / SayHelloJson',
cache:false,
contentType:'application / json; charset = utf-8',
success:function(data){
alert('成功');
},
错误:函数(错误){
alert(err.status);
}
});
}) ;

C#代码:

< pre lang =cs> [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script。 Services.ScriptService]
public class MyWebService:System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]
公共字符串SayHelloJson()
{
var data = new {Greeting =& quot; Hello& quot;,Name =& quot; test& quot; };

JavaScriptSerializer js = new JavaScriptSerializer();

返回js.Serialize(数据);
}
}< / pre>
.ajax({ type: 'GET', dataType: 'json', async: false, url: 'MyWebService.asmx/SayHelloJson', cache: false, contentType: 'application/json; charset=utf-8', success: function (data) { alert('success'); }, error: function (err) { alert(err.status); } }); }); C# code: <pre lang="cs">[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class MyWebService : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public string SayHelloJson() { var data = new { Greeting = &quot;Hello&quot;, Name = &quot;test&quot; }; JavaScriptSerializer js=new JavaScriptSerializer(); return js.Serialize(data); } }</pre>


请尝试如下。



注意:使用Post而不是Get。



Please try is as below.

Note:Use Post instead of Get.


这篇关于我试图从c#webmethod获取jquery ajax函数中的json字符串数组。我收到错误500 [暂停]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆