-1

I create static web method and then i try to call this into script like this

UPDATE SCRIPT

<script type="text/javascript">
    debugger;
    alert("1");
    $(function () {
        $.ajax({
            type: "GET",
            url: "Maintenance.aspx/data_call",
            //data: "",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (result) {
                alert("12");
                debugger;
                var re = JSON.parse(result.d).response;
                debugger;

                console.log(JSON.parse(result.d).response);
                debugger;
            },
            error: function (error) {
                alert(Error);
            }
        });
    });
</script>

UPDATE

code

 [WebMethod]
 public static string data_call()
    {
        string result="";
        Data td=new Data();
        List<spselect_data_Result> selectdata=td.spselect_data().ToList();
        DataTable dt=new DataTable();
        dt.Columns.Add("RegionID",typeof(int));
        dt.Columns.Add("Region",typeof(string));
        dt.Columns.Add("StartDate",typeof(DateTime));
        dt.Columns.Add("EndDate",typeof(DateTime));

        foreach(var add in selectdata)
        {
            dt.Rows.Add(add.RegionID,add.Region,add.StartDate,add.EndDate);
        }
        result=DataSetToJSON(dt);
        return result;
    }


      public static string DataSetToJSON(DataTable dt)
    {

        Dictionary<string, object> dict = new Dictionary<string, object>();

        object[] arr = new object[dt.Rows.Count + 1];

        for (int i = 0; i <= dt.Rows.Count - 1; i++)
        {
            arr[i] = dt.Rows[i].ItemArray;
        }

       // dict.Add(dt.TableName, arr);
        dict.Add("response", arr);

        JavaScriptSerializer json = new JavaScriptSerializer();
        return json.Serialize(dict);                               
    }

protected void Page_Load(object sender, EventArgs e)
        {
           // data();
        }

when i debug code then an alert show like this

function Error (){[native code]}

and when when i set debugger on jquery and check then debugger comes on alert 1 and then on this line $(function() { then after this directly execute on this line means ajax not call

first i try to display data on console

error on console Failed to load resource: the server responded with a status of 500 (Internal Server Error)

When I try this the call only shows alert("1"). alert("12") is not called. Where is the problem?

rebma amber
  • 35
  • 11

2 Answers2

0

This problem maybe caused by maxJsonLength property in web.config file. To solve the problem You can chnage the MaxJsonLength property on your web.config:

<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration>  

The MaxJsonLength property is an integer property that by default set to 102400(100k) and cannot be unlimited.

Hamid Nasirloo
  • 2,074
  • 2
  • 11
  • 12
0

I see some problems in the code you posted.

First of all you don't need type: "POST" as you are not posting/sending any data.

So update that portion in ajax request.

$(function() {
        $.ajax({
            type: "GET",
            url: "Maintenance.aspx/YourMethod",           
            contentType: "application/json;charset=utf-8",
            dataType: "json",  
            success: function (result) {
                alert("12");
                debugger;                 
            },
            error: function (error) {
                alert(Error);
            }
        });
});

Or else to work it with post you will have to set

data: "{}",

See instead of setting cache:false in ajax request set it from a common place like

$(document).ready(function() {
  $.ajaxSetup({ cache: false });
});

Next on server side no need to call the method data in page load.

Just write it directly in class outside page load.And add the attribute WebMethod from System.web.services.

[WebMethod]
public static string YourMethod()
{
return "whatever you want";    
}

Note : Also another thing I noticed that you made your method name as data,so my advice is change it to something meaningful as data is a ajax call parameter key and it could conflict.

Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42