【问题标题】:Can't get data in Firefox and IE9 but it works fine in Chrome and Safari无法在 Firefox 和 IE9 中获取数据,但在 Chrome 和 Safari 中运行良好
【发布时间】:2012-01-20 03:27:45
【问题描述】:

我有这个方法

 function callCommentservice() {
        try {

            // Comment Service Url
         var getCommentServiceUrl = self.commentsServiceUrl + self.getRating + "tenantId=" + self.tenantId + "&ratedObjectTypeId=" + self.ratedObjectTypeId + "&ratedObjectId=" + self.ratedObjectId + "&ratingType=" + self.ratingType + "&start=" + self.start + "&totalRecordsNeeded=" + self.totalRecordsNeeded;


         $.ajax({
     type: "GET",
             url: getCommentServiceUrl,
             contentType: "application/json",
             timeout: 5000,
             beforeSend: function (xhr) {
                 xhr.setRequestHeader("Accept", "application/json");
             },
         dataType: "json",
             success: function (data) {
                 alert("I m in here.............!!!!");

                 //                    if (success == data) {

                 // Assigning totalRecordsNeeded value to global variable pagination_parameters.pageSize
                 pagination_parameters.pageSize = self.totalRecordsNeeded;

                 //Printing on a console.
                 debug("get comments url: " + getCommentServiceUrl);

                 // Calling Service in order to get data.
                 //                        $.getJSON(getCommentServiceUrl + "&callback=?", function (data) {
                 var emptyCommentHyperlink = document.getElementById('emptyCommentHyperlink');
                 // when there is no comment available in a service than this block will work.
                 if (data.length == 0) {

                     emptyCommentHyperlink.style.display = 'block';
                     //emptyCommentHyperlink.onclick = addCommentsBox;
                     $("#emptyCommentHyperlink").click(addCommentsBox);
                 }
                 else {

                     emptyCommentHyperlink.style.display = 'none';
                 }

                 //printing on a console.
                 debug("Date from the get service:" + data);
                 //alert(data);

                 // Changing date format of a service element AddedDateTime.
                 for (var key = 0; key < data.length; key++) {


                     var dataArray = data[key];

                     // Printing on console.
                     debug("data Array after the data object:" + dataArray);
                     var jsonDate = dataArray.AddedDateTime; //AddedDateTime is the service element.'

                     // Printing on a console.
                     debug("converted Time" + jsonDate);

                     var oneSecond = 1000;       // milliseconds in one seconds
                     // changing format of the AddedDateTime
                     var date = new Date(parseInt(jsonDate.substring(6)));

                     var year = date.getFullYear(); // 2011
                     var day = date.getDay();
                     var exactdate = date.getDate();
                     var month = date.getMonth();
                     var hours = date.getHours();
                     var minutes = date.getMinutes();
                     var month_names_short = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

                     // Fromating Absolute time over tooltip.
                     if (hours > 12) {
                         hours = hours - 12;
                         var exact = zeroPad(hours) + ":" + zeroPad(minutes) + " PM" + ", " + exactdate + " " + month_names_short[month] + " " + year;
                     }
                     else {

                         var exact = zeroPad(hours) + ":" + zeroPad(minutes) + " AM" + ", " + exactdate + " " + month_names_short[month] + " " + year;
                     }

                     // subtracting current date with the comment date.
                     var finalDate = Math.abs(new Date() - date) / oneSecond;

                     dataArray.time_duration = new TimeSpan(finalDate).toString();

                     // Printing on a console.
                     debug("finalDate: " + dataArray.time_duration);

                     //alert(dataArray.time_duration);
                     dataArray.Absolute_Date = exact;

                 }


                 // Extracting TotalCount from service.
                 var totalComments = data[0].TotalCount;
                 // alert(totalComments);


                 //Calling paging Function
                 pages(totalComments);

                 //Making the commenttable empty.
                 $('#commentDiv').empty();

                 // Printing on a console.
                 debug("data before mactache" + data);

                 var comments = { "Comments": data };
                 var html = Mustache.to_html(self.template_html, comments);
                 var target_div = document.getElementById("commentDiv");
                 target_div.innerHTML = html;
                 //alert("target_div");


                 //                        });

             },

             //                },
             error: function (xhr, ajaxOptions, thrownError) {
                 debug(xhr);

                 var errorMsg = document.getElementById('ErrorLable');
                 errorMsg.style.display = "block";
             }
         });
        }

        catch (err) {
            //Printing on a console.
            debug("callCommentservice");
            debug(err);

        }
    }

此方法在 Chrome 和 Safari 中运行良好,但在 IE 9 和 firefox 中无法运行,我在 firefox 请求标头中收到错误 405,如下所示:-

允许响应标头
获取内容长度 1565 内容类型文本/html; charset=UTF-8 服务器 Microsoft-IIS/7.5 X-Powered-By ASP.NET 访问控制允许原始... * 访问控制允许方法... POST、GET、 选项访问控制允许头...内容类型, 接受 Access-Control-Max-Age 1728000 日期 2011 年 12 月 14 日星期三 10:30:43 GMT

请求标头主机 services.farooq.tv 用户代理 Mozilla/5.0 (Windows NT 6.1;WOW64;rv:8.0.1) Gecko/20100101 Firefox/8.0.1 接受 text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language en-us,en;q=0.5 Accept-Encoding gzip, deflate 接受字符集 ISO-8859-1,utf-8;q=0.7,*;q=0.7 连接保持活动 来源http://marketplace.softech-lp35.softech.us 访问控制请求我... GET Access-Control-Request-He...内容类型

【问题讨论】:

  • "无法在 firefox 中获取数据 .. 但在 .. fire fox 中工作正常"?
  • 标题格式错误并被截断。你能把它们发布得更具可读性吗?在任何情况下:HTTP 错误 405 听起来服务器对请求不满意,因此您可能需要询问其操作员。
  • 但是为什么操作员只对 firefox 和 I.E 不满意,为什么在 chrome 和 safari 的情况下不满意?????
  • @BASEER HAIDER:这对我们来说很可能是不可能的。这是一个特定于服务器应用程序的问题,而不是我们的服务器。你能发布来自 Firefox 和浏览器的(完整且可读的)请求和响应标头 内容吗?

标签: javascript ajax json jquery


【解决方案1】:

contentType: "application/json" 中存在问题,我已将其从 $.ajax 函数中删除,现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 2014-12-25
    • 2018-02-04
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    相关资源
    最近更新 更多