从jQuery调用web服务不会产生结果

时间:2011-10-18 22:23:55

标签: jquery asp.net web-services

我正在尝试从JQuery访问Web服务。它工作正常。我能够将数据保存到数据库。当我尝试从数据库中获取数据时,我没有看到结果。当我调试时,我看到控件遇到GetFileInfo(),但jQuery中的回调不起作用。我试着在火虫中唠叨我在萤火虫的反应中看不到任何东西。我做错了什么。

MyService.asmx:

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

.aspx页面中的jquery实现。

    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);

更新1:

Json回应。

{"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 个答案:

答案 0 :(得分:2)

您可能正在使用ASP.NET Web服务(asmx)。假设您从Web服务获得了正确的JSON(您应该能够从查看响应的firebug中验证这一点),我相信问题在于您的成功回调函数(尝试在其中设置断点)。在asmx服务中,实际结果被包装为结果JSON对象中的属性(以避免XSS),因此您的成功函数应该类似于

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

上述内容适用于asmx和WCF服务。

答案 1 :(得分:0)

是System.IO命名空间中的FileInfo类,还是具有相同名称的自定义类?

如果是这样,Web服务可能在尝试序列化数组时抛出异常,因为FileInfo类无法序列化,因为它没有无参数构造函数。