locked
ajax POST returning error function with the correct data RRS feed

  • Question

  • User-712624838 posted

    I have the following code that is called whenever a dropdown is selected on my website:

    Defailt.aspx page

                   $.ajax({
                         type: "POST",
                         url: "WebService1.asmx/HelloWorld",
                         data: "{}",
                         contentType: "application/json; charset=utf-8",
                         dataType: "json",
                         success: function (response)
                         {
                             var resp = $.parseJSON(response.d);
                             alert(response.d);
                         },
                         error: function (response) {
                             alert(response.d);
                         },
                    });


    [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string HelloWorld() { var data = new { Greeting = "Hello", Name = "How are you doing" }; System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); return js.Serialize(data); } }

    Whenever the ajax function is called I use Google Chromes debugging tools to put breakpoints and I see that:

    1.  The error function gets called every time

    2. The response.status is 200 and response.StatusText is "OK"

    3. responseText="<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">{"Greeting":"Hello","Name":"How are you doing"}</string>"

    Why is it returning the correct data in the 'error' function?

    How can I get it to return success??

    EDIT

    Whenever I change

    dataType: "json",

    to

    dataType: "text",

    my success function is now called and the response contains the following:

    "<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">{"Greeting":"Hello","Name":"How are you doing"}</string>"

    This is strange,

    I need to figure out how I can get dataType: "json" to work and actually return json data.

    Wednesday, December 10, 2014 1:07 PM

Answers

  • User-1598917946 posted

    Did you try putting a breakpoint at your webservice method to see if it gets called .. if yes try putting an alert in success like ..

    $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/HelloWorld",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                       alert(data.d.Greeting);
                        alert(data.d.Name);
                    },
                    failure: function (data, success, failure) {
                        alert("Error:" + failure);
                    }
                });
     [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
            [WebMethod]
            [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public object HelloWorld()
            {
                var data = new { Greeting = "Hello", Name = "How are you doing" };
               
                return data;
            }
        }


    Change the return type from string to object ..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 11, 2014 6:53 AM

All replies

  • User1918509225 posted

    Hi Wanting2Learn,

    First ,For Chromes is not supported here , I suggest that you can   try to run it in IE to check if it work here.

    Second, I suggest that you can Press F12 in IE to debug  your javascript to check if it has any error message.

    Best Regards,

    Kevin Shen.

    Thursday, December 11, 2014 4:58 AM
  • User-1598917946 posted

    You don't need to do parse , you can directly access your ob

    $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/HelloWorld",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        data.d.Greeting;
                        data.d.Name;
                    },
                    failure: function (data, success, failure) {
                        alert("Error:" + failure);
                    }
                });
     [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
            [WebMethod]
            [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public objectHelloWorld()
            {
                var data = new { Greeting = "Hello", Name = "How are you doing" };
               
                return data;
            }
        }
    



    ject ...

    Thursday, December 11, 2014 5:10 AM
  • User-712624838 posted

    Hi Kevin, I have tried it in IE and it doesnt work either.  I get the same behaviour when I check the debugger also.

    I'm puzzled as to why it would work on my local PC in Chrome/IE but not on a live web server.

    Thanks

    Thursday, December 11, 2014 5:48 AM
  • User-712624838 posted

    Hi Vimal, I changed my code to the same as yours and it still does not work.

    Neither the success function or the failure function gets called.

    Any other ideas?

    Thanks

    Thursday, December 11, 2014 5:49 AM
  • User-712624838 posted

    Please see my EDIT to the original post everyone.

    Thursday, December 11, 2014 6:21 AM
  • User-1598917946 posted

    Did you try putting a breakpoint at your webservice method to see if it gets called .. if yes try putting an alert in success like ..

    $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/HelloWorld",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                       alert(data.d.Greeting);
                        alert(data.d.Name);
                    },
                    failure: function (data, success, failure) {
                        alert("Error:" + failure);
                    }
                });
     [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
            [WebMethod]
            [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public object HelloWorld()
            {
                var data = new { Greeting = "Hello", Name = "How are you doing" };
               
                return data;
            }
        }


    Change the return type from string to object ..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 11, 2014 6:53 AM