0

I am trying to access a webservice from JQuery. It works fine.I was able to save data to the database. While I try to get data from database I don't see the result. When I debug I see the control hitting GetFileInfo() but the callback in the jQuery is not working. I tried bebugging in fire bug I see nothing under the Response in the firebug. What am I doing wrong.

MyService.asmx:

 [WebMethod]
        public FileInfo[] GetFileInfo(int Id)
        {
            Proxies.ServiceRef.ServiceClient c = new Proxies.ServiceRef.ServiceClient();
            return c.GetFileInfo(Id).ToArray();
        }

jquery implementation in .aspx page.

    this.ServiceProxy = function (serviceUrl) {
          var _I = this;
        this.serviceUrl = serviceUrl;
        this.isWcf = false;
        this.timeout = 10000;
        this.contentType = "application/json";

      this.invoke = function (method, params, callback, errorHandler) {
      var jsonData = _I.isWcf ? JSON.stringifyWcf(params) : JSON.stringify(params);

            // Service endpoint URL        
            var url = _I.serviceUrl + method;

            $.ajax({
                url: url,
                data: jsonData,
                type: "POST",
                contentType: _I.contentType,
                timeout: _I.timeout,
                dataType: "serviceproxy",  // custom type to avoid double parse
                dataFilter: function (jsonString, type) {
                    if (type == "serviceproxy") {
                        // Use json library so we can fix up dates        
                        var res = JSON.parseWithDate(jsonString);
                        if (res && res.hasOwnProperty("d"))
                            res = res.d;
                        return res;
                    }
                    return jsonString;
                },
                success: function (result) {
                    if (callback)
                        callback(result);
                },
                error: function (xhr, status) {
                    var err = null;
                    if (xhr.readyState == 4) {
                        var res = xhr.responseText;
                        if (res && res.charAt(0) == '{' && status != "parsererror")
                            var err = JSON.parse(res);
                        if (!err) {
                            if (xhr.status && xhr.status != 200)
                                err = new CallbackException(xhr.status + " " + xhr.statusText);
                            else {
                                if (status == "parsererror")
                                    status = "Unable to parse JSON response.";
                                else if (status == "timeout")
                                    status = "Request timed out.";
                                else if (status == "error")
                                    status = "Unknown error";
                                err = new CallbackException("Callback Error: " + status);
                            }
                            err.detail = res;
                        }
                    }
                    if (!err)
                        err = new CallbackException("Callback Error: " + status);

                    if (errorHandler)
                        errorHandler(err, _I, xhr);
                }
            });
        }
    }

var serviceUrl = "service/MyService.asmx/";
var proxy = new ServiceProxy(serviceUrl);

 function GetFileInfo() {

            proxy.invoke("GetFileInfo",
            { Id: $("#IAssignmentId").val() },
             function (result) {              
                    $.each(result, function (index) {

                  alert (this.Filename);

                 });
             }, onPageError);

Update 1 :

Json Response.

{"d":[{"__type":"Proxies.AFARServiceRef.AssignmentInfo","ExtensionData":{},"AssignDate":"\/Date(1317748587667)\/","AssignFileName":null,"ClaimId":"PA026195","ClaimantName":"Rachel Weiss","FirstContactDate":"\/Date(1302678000000)\/","FirstContactTime":{"Ticks":433800000000,"Days":0,"Hours":12,"Milliseconds":0,"Minutes":3,"Seconds":0,"TotalDays":0.50208333333333333,"TotalHours":12.049999999999999,"TotalMilliseconds":43380000,"TotalMinutes":723,"TotalSeconds":43380},"Id":5257,"InspectionDate":"\/Date(1302246000000)\/","StatusId":1,"SubmittedCount":5,"UploadedCount":9}]}

2 Answers 2

2

It appears that you are probably using ASP.NET web service (asmx). Assuming that you get the correct JSON from the web service (you should able to verify this from firebug looking at the response), I believe that problem lies with your success callback function (try setting break-point into it). In asmx services, the actual result is wrapped as a property in a resultant JSON object (to avoid XSS), so your success function should be something like

success: function (result) {
    if (result) {
       // unwrap result
       result = result.d != undefined ? result.d : result;
    }
    if (callback)
        callback(result);
},

Above will work for both asmx and WCF services.

Sign up to request clarification or add additional context in comments.

3 Comments

Vinay thank you. When I debug using firebug I do see the response and the status is 200 ok. I placed a break point at Success. But as soon as I hit F10, once the control reaches Success:, the control jumps to error:. I attached the Json response to the question.
@BumbleBee, setting break-point at success: will be hit when web-service is called. You need to set break-point within the success or callback function - say at line $.each(result, function (index) {. On the related note, your JSON response does not have FileName property, so the line alert (this.Filename); is going to cause problem. Change that to alert(this.AssignFileName); but mind you, your sample response has null value for the same.
Placed a break point as mentioned but never hits the break point. I am clueless how to go about this.
0

Is the FileInfo class from the System.IO namespace, or a custom class with the same name?

If so, it is likely the web service is throwing an exception when trying to serialize the array, as the FileInfo class cannot be serialized because it does not have a parameterless constructor.

1 Comment

Thank you.when I debug in the firebug I see the error being thrown at success: function (result) {}. I can't figure out what the error is. When i tried to stepinto it is not stepping into error fucntion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.