加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何在Firefox中解码来自jQuery $.ajax请求的XML响应

发布时间:2020-12-16 03:51:41 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试为WebService创建一个ajax请求,该请求返回给定 XML中指定参数的数据.这似乎在IE中运行良好,但Firefox无法解码响应.解码后我也可以在Fiddler中成功查看响应.这是代码: $(function() { $.ajax({ type: "GET",url: 'http:/localhost/webservice.asmx
我正在尝试为WebService创建一个ajax请求,该请求返回给定 XML中指定参数的数据.这似乎在IE中运行良好,但Firefox无法解码响应.解码后我也可以在Fiddler中成功查看响应.这是代码:

$(function() {
    $.ajax({
        type: "GET",url: 'http:/localhost/webservice.asmx/GetTags?groupId=10',contentType: "text/xml; charset=utf-8",dataType: "xml",success: function(response) {
            $('#result').html('success',response);
            $(response).find("string").each(function() {
                $('#result').append($(this).text());
            });
        },error: function(response) {
            $('#result').html('failure',response);
        }
    });

});

有没有办法指定需要解码响应?或任何其他方式使其工作?

编辑:
@ Nikki9696 – 它不是JSON编码的,因为数据是以XML格式返回的.

@Oleg – 如果通过URL访问webservice,我可以在浏览器中看到的示例XML如下:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <string>tag 1</string>
  <string>tag 2</string>
  <string>tag 3</string>
</ArrayOfString>

TextView中的提琴手返回 和一条消息

“Response is encoded and may need to
be decoded before inspection. Click
here to transform.”

单击后,它将显示相同的XML.我在IIS中关闭动态内容压缩然后直接在fiddler中看到XML,但FF仍然无法应对,因此排除了压缩.

我玩了一下脚本,似乎jQuery可以默认或猜测一些参数,所以dataType,例如,不是必须的.通过这些设置,我获得了成功消息,但它仍然不知道如何处理数据.我尝试将dataType设置为“jsonp”,如某些SS线程中所建议的那样(此时无法找到它,当我这样做时会链接它)并且错误更改为缺失;在声明之前,我想因为它不是JSON对象,而是XML.有没有办法设置webservice来返回JSON?

编辑2:我更新了网址以反映实际发生的情况.对不起,我错过了,让任何人都无法发现它.

解决方法

因为您使用像’/webservice.asmx/GetTags?groupId=10’这样的相对URL,所以您对不同的域没有任何问题.在我看来,你应该只修复你的JavaScript代码.例如以下代码

$(function () {
    $.ajax({
        type: "GET",url: '/WebService1.asmx/GetTags',data: {groupId:10},success: function (response) {
            $('#result').html('success:');
            $(response).find("string").each(function () {
                $('#result').append('<br />'+$(this).text());
            });
        },error: function (response) {
            $('#result').html('failure:<br />' + response.responseText);
        }
    });
});

在Internet Explorer,Firefox和Google Chrome中运行良好.如果您需要,我可以发布您可以下载整个Visual Studio 2010项目的URL.

更新:要从Web方法返回JSON而不是XML,您可以将[ScriptMethod(UseHttpGet = true)]替换为[ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)]属性(在.NET 4.0中,您也可以这样做以不同的方式)并将JavaScript代码修改为以下内容

$(function () {
    $.ajax({
        type: "GET",url: '/WebService1.asmx/GetTagsJson',contentType: "application/json; charset=utf-8",data: { groupId: 10 },//dataType: "xml",success: function (response) {
            $('#result').html('success:');
            $(response.d).each(function () {
                $('#result').append('<br />' + this);
            });
        },error: function (response) {
            $('#result').html('failure:<br />' + response.responseText);
        }
    });
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读