locked
ajax script alert query RRS feed

  • Question

  • User639567535 posted

    I create static web method and then i try to call this into script like this<o:p></o:p>

    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>
    
    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?



    Monday, October 3, 2016 10:14 AM

Answers

All replies

  • User-484054684 posted

    Put a debugger on this piece of code and see the exact details in error object. That should give the clue. Also put break point in C# code and see if it hits.

    error: function (error) {
    debugger; alert(Error); }
    Monday, October 3, 2016 10:28 AM
  • User639567535 posted

    i also but break-point but this shows first alert ("1") and then  direct execute to last line which is this

     </script>

    Monday, October 3, 2016 10:37 AM
  • User639567535 posted

    when i try this 

    error: function (error) {
    debugger;
    var r = error.responseText;
    var errorMessage = r.Message;
    alert(errorMessage);
    alert(r);
    alert(Error);
    }

    i got this error

    Monday, October 3, 2016 11:00 AM
  • User475983607 posted

    Enable HTTP GET.

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string data_call()

    Monday, October 3, 2016 11:29 AM
  • User639567535 posted

    Enable HTTP GET.

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string data_call()

    this show error on this

    ScriptMethod
    Monday, October 3, 2016 12:08 PM
  • User-484054684 posted

    If it's the method in your asmx webservice:

    1. If you are getting compilation error - then, make sure you refer the ScriptMethod with corresponding using directive.

    2. Also check if you have this entry (if not try create one) in web.config

    <webServices>
            <protocols>
                  <add name="HttpGet"/>
            </protocols>
    </webServices>

    Reference:

    http://codeclimber.net.nz/archive/2007/12/03/UseHttpGet-needs-you-to-enable-HttpGet-in-the-web.config.aspx 

    If it's a page method (I hope it is, in your case?):

    POST only would work due to security restrictions as per this reference http://stackoverflow.com/questions/2397466/ajax-get-requests-to-an-asp-net-page-method/2397521#2397521.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 3, 2016 12:19 PM
  • User475983607 posted

    Bakhtawar Ashiq

    this show error on this

    I tested it and it worked fine.

    What is the error? Is this a compiler error?  Did you remember to add the appropriate using statement and/or library reference?

    Monday, October 3, 2016 1:28 PM