Message from webpage undefined

我怕爱的太早我们不能终老 提交于 2020-01-06 04:42:23

问题


I am returning a simple string from a webmethod to a Javascript function.

I am using an AJAX enabled website in ASP.NET 2.0. I get the date in firefox but inside IE 8 it returns undefined.

Do I have to parse the string in the JSON format using some serialize class? In my webmethod, I am just using:

    return DateTime.Now.ToString();

 $(document).ready(function(){
     var pageUrl = '<%=ResolveUrl("~/test/test.aspx")%>';

        // Test
        $('#<%=trgNo.ClientID%>').change(function(){
            var trgId = $(this+'input:checked').val();

            $.ajax({
                type: "POST",
                url : pageUrl+ '/getDet',
                data : '{categ: "' +trgId + '"}',
                contentType:"application/json; charset=utf-8",
                dataType:"json",
                success:OnSuccess,
                failure: function(msg){
                    if(msg.hasOwnProperty("d"))
                        alert(msg.d);   
                    else
                        alert('error fetching values from database');
                   }
                });
        });

        function OnSuccess(msg)
        {
        if(msg.hasOwnProperty("d"))
            alert(msg.d);
        else
            alert(msg);
        }
});

Edit It seems the success function is firing the problem is with response 'alert(msg)' works in firefox but not in IE 8 with asp.net 2.0


回答1:


Maybe you dont want to use this, but I´m very happy with the asp net ajax build in function, since it builds a header, that works properly on browsers.

$(document).ready(function(){
     var pageUrl = '<%=ResolveUrl("~/test/test.aspx")%>';

        // Test
        $('#<%=trgNo.ClientID%>').change(function(){
            var trgId = $(this+'input:checked').val();

            var proxy = Sys.Net.WebServiceProxy;
            proxy.invoke("",                // if current page "", if webservice "/srv.asmx"
                         "getDet",              //method name
                         false,                 //post = true, get = false 
                         { categ : trgId },     //javascript object
                         OnSuccess,             // Success Function
                         onError,               // Error Function
                         { yourOwn : userData } // Custom User Data to Handler
            );

        });

        function OnSuccess(response, usercontext)
        {

            // usercontext.yourOwn === userData;

            // response is sent WITHOUT "d", it is removed internally by the proxy 

        alert(response);
        }

});

Dont forget to include the ScriptManager...



来源:https://stackoverflow.com/questions/4848617/message-from-webpage-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!