0

I am creating a webservice in asp.net. 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?

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetQuestions()
{
    return new JavaScriptSerializer().Serialize(Biz.BAL.GetQuestions());
}
1
  • How are you getting the above response? Note that if you test the webservice through your webbrowser you will get a different result than when you do an actuall ajax call. Commented Oct 3, 2012 at 12:15

3 Answers 3

1

Could you verify you web.config if the following is correct:

1) "Rerouting the *.ASMX httphandler to 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:

    <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>
Sign up to request clarification or add additional context in comments.

2 Comments

other thought... did you already add the attribute [System.Web.Script.Services.ScriptService] to your webservice ? (presumably yes since you already call a ScriptMethod.. )
Final thought = rerouting asmx httphandler.. I've edited my answer for that. If that does not help you, I'm clueless, sorry
1

Please use these lines in 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)]));

Comments

0

Using this code is safe and convinient: ASP

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

in Javascript I think you should do somthing like this

var start = YOUR_RESPONSE.indexOf('<string ');
start = YOUR_RESPONSE.indexOf('>', start)+ 1;
YOUR_RESPONSE.substring(start, YOUR_RESPONSE.lastIndexOf('</string>'));

7 Comments

XmlDocument x= new XmlDocument(); x.LoadXml(new javaScriptSerializer(). Serialize(Biz.BAL.GetQuestions().ToString())); return x.DocumentElement.InnerText;
above asp code is not working...its throwing xml exception Data at the root level is invalid. Line 1, position 1.
I have tested this right now with your example and it's work. What happended exactly? Did Compilation error occure or run time exception?
Please debug your code and check if your data had an invalid character?
And in JS alert data before each line to debug them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.