首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在asp.net中创建web服务时删除xml标记

在asp.net中创建web服务时删除xml标记
EN

Stack Overflow用户
提问于 2012-10-03 17:18:15
回答 3查看 3.5K关注 0票数 0

我正在用asp.net创建一个webservice服务。下面是我得到的回复。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"Question1":"do the like the idea of the first speaker?","Option1":"YES","Option2":"NO","Option3":"NOT SURE","Option4":"","Option5":"","Type":"button","QID":"q1"}</string>

代码语言:javascript
运行
复制
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetQuestions()
{
    return new JavaScriptSerializer().Serialize(Biz.BAL.GetQuestions());
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-03 18:52:54

如果以下内容正确,您能否验证您的web.config:

1) "Rerouting the *.ASMX httphandler to ScriptHandlerFactory“

代码语言:javascript
运行
复制
    <httpHandlers>
        <remove path="*.asmx" verb="*"/>
        <add path="*.asmx" verb="*" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpHandlers>

2) "Json序列化“您可以将jsonSerialisation设置添加到您的web.config中,以确保Json序列化正常工作:

代码语言:javascript
运行
复制
    <configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/></sectionGroup></sectionGroup></sectionGroup>
    </configSections>
票数 1
EN

Stack Overflow用户

发布于 2015-01-01 12:40:57

请在Web方法中使用以下行:

代码语言:javascript
运行
复制
 JavaScriptSerializer Machinejson = new JavaScriptSerializer();
            this.Context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
            this.Context.Response.ContentType = "application/json; charset=utf-8"; // to remove xml tag from response
            this.Context.Response.Write(Machinejson.Serialize([Object or string to return in form of json)]));
票数 1
EN

Stack Overflow用户

发布于 2012-10-03 17:32:58

使用以下代码是安全和方便的: ASP

代码语言:javascript
运行
复制
var X = new XmlDocument();
X.LoadXml(YOUR_RESPONSE);
return X.DocumentElement.InnerText;

在Javascript中,我认为你应该这样做

代码语言:javascript
运行
复制
var start = YOUR_RESPONSE.indexOf('<string ');
start = YOUR_RESPONSE.indexOf('>', start)+ 1;
YOUR_RESPONSE.substring(start, YOUR_RESPONSE.lastIndexOf('</string>'));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12705380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档