0

I have a web form IndexPage.aspx. The script part of this front end code is calling a code-behind function using ajax call.Here's the code:

       $.ajax({
                type: "POST",
                url: '<%=ResolveUrl("~//IndexPage.aspx//SaveXml")%>',
                data: JSON.stringify(logic),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                //async: true,
                //cache: false,
                success: function (msg) {
                    alert("AJAX received  : " + msg.d);
                },
                error: function (msg) {
                    alert("Failed  : " + msg.d);
                }                    


            });

The "Failed" alert box is shown with return value 'undefined' after I run the code.

The code behind function is:

    public static string SaveXml(string logic)
    {

        return logic;
    }

This code-behind function is not getting called. The breakpoint which I have set at the code-behind function is not getting hit. I have tried almost all solutions from stack overflow. Please see if anythig is wrong in this code.

  • msg.d will never exist in the error response. Check http://api.jquery.com/jquery.ajax/ for what _is_ returned from the call in the error callback. Alternatively look in your browser's console and network tools to see what the response status and content was. That should then point you in the right direction to find the error. – ADyson Feb 13 '18 at 16:37
  • Hi, I checked the browser 'Response body' in Network. It is showing error: ArgumentException: Unknown web method SaveXml – Nikhil Patil Feb 13 '18 at 17:22

2 Answers2

0

Thanks friends,

The issue was resolved. Checking browser console and network helped me a lot to solve the error.

I did two changes

1) Added [System.Web.Services.WebMethod] to code-behind function

2) Changed my => data: JSON.stringify(logic) ..... to ....=> data: '{ logic:\''+ logic +'\'}'

I did the second change after referring to this link: "Invalid JSON primitive" in Ajax processing While sending just a string you need to add escape sequence.

  • `data: '{ logic:\''+ logic +'\'}'` ...yuk. No need to build JSON by hand like that. More robust would be `data: JSON.stringify({ "logic": logic });` – ADyson Feb 13 '18 at 21:36
-1

Resolved

Ajax call:

 $.ajax({
        type: "POST",
        url: '<%=Page.ResolveUrl("~/WebForm1.aspx/SaveXml")%>',
        data: JSON.stringify({ "logic": "logic value" }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        //async: true,
        //cache: false,
        success: function (msg) {
            alert("AJAX received  : " + msg.d);
        },
        error: function (msg) {
            alert("Failed  : " + msg.d);
        }


    });

Web Method

  [WebMethod]
    public static void SaveXml(string logic)         {
    }

Web.config

      <system.web>
        <webServices>
        <protocols>
             <add name="HttpGet"/>
             <add name="HttpPost"/>
        </protocols>
    </webServices>
  • 3
    This doesn't answer the question. Once you have enough rep (50 I think, not a lot) you can write comments, until then, refrain from writing answers which are not answers :-) – ADyson Feb 13 '18 at 16:37
  • I checked the browser console. (I assume this is what u meant to check). It is showing Unexpected token @ – Nikhil Patil Feb 13 '18 at 16:49
  • Please change url to var pageUrl = '<%=ResolveUrl("~/IndexPage.aspx")%>' url: pageUrl +"/SaveXml", – Arihant Jain Feb 13 '18 at 16:56