Click here to Skip to main content
15,915,759 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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>
Posted

1 solution

Please try is as below.

Note:Use Post instead of Get.

$(document).ready(function () {
    $.ajax({
        type: 'POST',
        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);
        }
    });
});


Check for more info : 3 mistakes to avoid when using jQuery with ASP.NET AJAX
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900