[英]Remove xml tag while creating web service in asp.net
I am creating a webservice in asp.net. 我正在asp.net中创建一个web服务。 Below is the response I'm getting.
以下是我得到的回应。
<?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>
How can I remove the xml tag from here...any ideas? 如何从这里删除xml标签......任何想法?
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetQuestions()
{
return new JavaScriptSerializer().Serialize(Biz.BAL.GetQuestions());
}
Could you verify you web.config if the following is correct: 如果以下内容正确,您可以验证web.config:
1) "Rerouting the *.ASMX httphandler to ScriptHandlerFactory" 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 serialization" You can add the jsonSerialisation setting to your web.config to make sure the Json serialization works: 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>
Please use these lines in Web Method: 请在Web方法中使用这些行:
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)]));
Using this code is safe and convinient: ASP 使用此代码是安全和方便的:ASP
var X = new XmlDocument();
X.LoadXml(YOUR_RESPONSE);
return X.DocumentElement.InnerText;
in Javascript I think you should do somthing like this 在Javascript中我认为你应该做这样的事情
var start = YOUR_RESPONSE.indexOf('<string ');
start = YOUR_RESPONSE.indexOf('>', start)+ 1;
YOUR_RESPONSE.substring(start, YOUR_RESPONSE.lastIndexOf('</string>'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.