简体   繁体   English

ASP.net中的JQuery Ajax调用错误

[英]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, 我在asp.net中使用jquery ajax调用,我有一个带有某些参数的静态Web方法,当我尝试调试其未命中的方法时,我在错误日志中看到其显示parseError,我删除了所有参数并进行了检查,但是还是一样的错误,

[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电话

 $.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 所有参数正确传递

Change the Access Modifier 更改访问修饰符

private static void AddData()

to

public static void AddData()

All parameters going to json data like so: 所有参数都将传递给json数据,如下所示:

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());
                }
            });

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());
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM