0

我正在 asp.net 中创建一个 web 服务。以下是我得到的回应。

<?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>

我怎样才能从这里删除 xml 标签......有什么想法吗?

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetQuestions()
{
    return new JavaScriptSerializer().Serialize(Biz.BAL.GetQuestions());
}
4

3 回答 3

1

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

1)“将 *.ASMX httphandler 重新路由到 ScriptHandlerFactory”

    <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 序列化工作:

    <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>
于 2012-10-03T10:52:53.653 回答
1

请在 Web Method 中使用这些行:


 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)]));
于 2015-01-01T04:40:56.817 回答
0

使用此代码既安全又方便:ASP

var X = new XmlDocument();
X.LoadXml(YOUR_RESPONSE);
return X.DocumentElement.InnerText;

在Javascript中我认为你应该做这样的事情

var start = YOUR_RESPONSE.indexOf('<string ');
start = YOUR_RESPONSE.indexOf('>', start)+ 1;
YOUR_RESPONSE.substring(start, YOUR_RESPONSE.lastIndexOf('</string>'));
于 2012-10-03T09:32:57.557 回答