jQuery ajax request response is empty in Internet Explorer

时间:2022-10-08 23:11:02

I'm doing the following ajax call:

我正在做以下的ajax调用:

//exif loader
function LoadExif(cImage) {
    $.ajax({
        type: "POST",
        url: "http://localhost:62414/Default1.aspx/GetImageExif",
        data: "{iCurrentImage:" + cImage + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: ajaxFailed,
        success: function (data, status) {
            var sStr = '';
            for (var count in data.d) {
                sStr = sStr + data.d[count];
            };
            alert(sStr);              
        }
    });
};

In Firefox the request works really fine. When I try to run the code in Internet Explorer, the response is empty.

在Firefox中,请求工作正常。当我尝试在Internet Explorer中运行代码时,响应为空。

Here is the webmethod witch is called:

这是webmethod女巫被称为:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetImageExif(ByVal iCurrentImage As Integer) As String
    Dim sbTable As New StringBuilder
    sbTable.AppendLine("<table>")
    sbTable.AppendLine("<tr>")
    sbTable.AppendLine("<td>Name</td><td>" & gGallery.Images(iCurrentImage).File.Name & "</td>")
    sbTable.AppendLine("</tr>")
    sbTable.AppendLine("</table>")
    Return sbTable.ToString
End Function

Any ideas?

Jan

2 个解决方案

#1


1  

When you have alert(data); what do you see?

当你有警报(数据);你看到了什么?

Thanks for this hint. This was the issue. I've copied the code from another of my projects.

谢谢你的提示。这就是问题所在。我从我的另一个项目中复制了代码。

alert(data.d);

... works in both browsers. Thanks for the quick answers.

...适用于两种浏览器。谢谢你的快速回答。

Jan

#2


1  

Starting with ASP.NET 3.5 MS introduced a security feature where they encapsulate any JSON response in a parent object ("d").

从ASP.NET 3.5开始,MS引入了一个安全功能,它将任何JSON响应封装在父对象(“d”)中。

Doing this helps against a XSS vulnerability (described here: http://haacked.com/archive/2009/06/25/json-hijacking.aspx)

这样做有助于抵御XSS漏洞(此处描述:http://haacked.com/archive/2009/06/25/json-hijacking.aspx)

That's the reason why it exists.

这就是它存在的原因。

Here's how to handle it, if you code against multiple versions of ASP.NET, you can simply use the following check in your success function to detect it's existence:

以下是如何处理它,如果您针对多个版本的ASP.NET进行编码,您只需在成功函数中使用以下检查来检测它的存在:

if (data.hasOwnProperty('d'))
  //use data.d in your code;
else
  //use data in your code

As for why Firefox handles it appropriately and IE8 does not, I would assume it has something to do how each parses the JSON object.

至于为什么Firefox适当地处理它而IE8没有处理它,我会假设它有一些关于每个解析JSON对象的事情。

#1


1  

When you have alert(data); what do you see?

当你有警报(数据);你看到了什么?

Thanks for this hint. This was the issue. I've copied the code from another of my projects.

谢谢你的提示。这就是问题所在。我从我的另一个项目中复制了代码。

alert(data.d);

... works in both browsers. Thanks for the quick answers.

...适用于两种浏览器。谢谢你的快速回答。

Jan

#2


1  

Starting with ASP.NET 3.5 MS introduced a security feature where they encapsulate any JSON response in a parent object ("d").

从ASP.NET 3.5开始,MS引入了一个安全功能,它将任何JSON响应封装在父对象(“d”)中。

Doing this helps against a XSS vulnerability (described here: http://haacked.com/archive/2009/06/25/json-hijacking.aspx)

这样做有助于抵御XSS漏洞(此处描述:http://haacked.com/archive/2009/06/25/json-hijacking.aspx)

That's the reason why it exists.

这就是它存在的原因。

Here's how to handle it, if you code against multiple versions of ASP.NET, you can simply use the following check in your success function to detect it's existence:

以下是如何处理它,如果您针对多个版本的ASP.NET进行编码,您只需在成功函数中使用以下检查来检测它的存在:

if (data.hasOwnProperty('d'))
  //use data.d in your code;
else
  //use data in your code

As for why Firefox handles it appropriately and IE8 does not, I would assume it has something to do how each parses the JSON object.

至于为什么Firefox适当地处理它而IE8没有处理它,我会假设它有一些关于每个解析JSON对象的事情。