ASMX方法向客户端返回XML而不是JSON



在ASMX中编写一个Web方法,将JSON数据返回到Angular应用程序。下面是Webmethod看起来像的样子

[WebMethod]        
[ScriptMethod(UseHttpGet =true,ResponseFormat =ResponseFormat.Json)]
public MyProjects getReport(int reportId)
{
}

以下是我对Angular 的称呼

this.client.get('http://localhost:51071/webservice1.asmx/getReport?reportId=596', 
{ withCredentials: true, responseType: 'text' })
.subscribe( (data) => console.log(data));

但问题是angular应用程序接收到的数据是XML格式的。这就是我在那里指定文本的原因。如果我在那里指定json,它当然会触发错误,说json数据无效。

我完全不知道我做错了什么,也不知道要更改什么才能获得纯JSON

以下是控制台输出看起来像的样子

<?xml version="1.0" encoding="utf-8"?>
<Projects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Success>true</Success>
<TotalRecords>391</TotalRecords>
<Data>
<ProjectData>
<ProjectCode>E6012</ProjectCode>

您确定您的IIS配置吗?

打开IIS管理器显示IIS服务器的属性单击MIME类型,然后查找具有JSON扩展名的行:文件扩展名:.jsonMIME类型:application/json

如果找不到,请创建它。

返回IIS服务器的属性单击处理程序映射添加脚本映射请求路径:*.json可执行文件:C:\WINDOWS\system32\inetsrv\asp.dll名称:JSON

我希望这能帮助你

最新更新