Syed Salman Raza Zaidi
Syed Salman Raza Zaidi

Reputation: 2192

JQuery Ajax call error in ASP.net

I am using jquery ajax call in asp.net,I have a static web method with some parameters, When I am trying to debug its not hitting the method, I saw in error log its showing parseError, I removed all parameters and checked,but still same error,

[WebMethod]
  private static void AddData(int type, int categ, string desc, string date, string city, string state)

        {
//Do Processing
        }

I also tried with this,but same error

[WebMethod]
        private static void AddData()
        {
//do Processing
        }

This is my ajax call

 $.ajax({
                type: "POST",
              url: 'MyPage.aspx/AddData?type=' + encodeURIComponent(crimetype) + "&categ=" + encodeURIComponent(crimecateg) + "&desc=" + encodeURIComponent(desc) + "&date=" + encodeURIComponent(crimedate) + "&city=" + encodeURI(city) + "&state=" + encodeURIComponent(stateid),

                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                },
                error: function (data, errorThrown) {
                    debugger
                    alert(errorThrown);
                    alert(data.toString());
                }
            });

I tried with this as well

$.ajax({
                type: "POST",
                url: 'MyPage.aspx/AddData',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                },
                error: function (data, errorThrown) {
                    debugger
                    alert(errorThrown);
                    alert(data.toString());
                }
            });

All parameters are passing correctly

Upvotes: 0

Views: 615

Answers (3)

Rohan
Rohan

Reputation: 703

Try this.

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static void AddData()
{
   //Your logic
}

And during call

$(document).ready(function () {
            InsertData();
        });
        function InsertData() {
            $.ajax({

                type: "POST",

                contentType: "application/json; charset=utf-8",

                url: "MyPage.aspx/AddData",

                data: "{}",

                dataType: "json",
                success: function (response) {}
                error: function (data, errorThrown) {
                    debugger
                    alert(errorThrown);
                    alert(data.toString());
                }
            });

Upvotes: 0

iJade
iJade

Reputation: 23791

Change the Access Modifier

private static void AddData()

to

public static void AddData()

Upvotes: 1

user1956570
user1956570

Reputation: 142

All parameters going to json data like so:

var jsonData = {
    categ: value,
    desc: value,
    // and others
}

$.ajax({
                type: "POST",
                url: 'MyPage.aspx/AddData'
                data: jsonData //your parameters
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                },
                error: function (data, errorThrown) {
                    debugger
                    alert(errorThrown);
                    alert(data.toString());
                }
            });

Upvotes: 0

Related Questions