asp.net asmx web service returning xml instead of json












49















Why does this simple web service refuse to return JSON to the client?



Here is my client code:



        var params = { };
$.ajax({
url: "/Services/SessionServices.asmx/HelloWorld",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
timeout: 10000,
data: JSON.stringify(params),
success: function (response) {
console.log(response);
}
});


And the service:



namespace myproject.frontend.Services
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SessionServices : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
}
}


web.config:



<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>


And the response:



<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>


No matter what I do, the response always comes back as XML. How do I get the web service to return Json?



EDIT:



Here is the Fiddler HTTP trace:



REQUEST
-------
POST http://myproject.local/Services/SessionServices.asmx/HelloWorld HTTP/1.1
Host: myproject.local
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://myproject.local/Pages/Test.aspx
Content-Length: 2
Cookie: ASP.NET_SessionId=5tvpx1ph1uiie2o1c5wzx0bz
Pragma: no-cache
Cache-Control: no-cache

{}

RESPONSE
-------
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 19 Jun 2012 16:33:40 GMT
Content-Length: 96

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>


I have lost count of how many articles I have read now trying to fix this. The instructions are either incomplete or do not solve my issue for some reason.
Some of the more relevant ones include (all without success):




  • ASP.NET web service erroneously returns XML instead of JSON

  • asmx web service returning xml instead of json in .net 4.0

  • http://williamsportwebdeveloper.com/cgi/wp/?p=494

  • http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/

  • http://forums.asp.net/t/1054378.aspx

  • http://jqueryplugins.info/2012/02/asp-net-web-service-returning-xml-instead-of-json/


Plus several other general articles.










share|improve this question

























  • I see the target framework tag set to 4.0, what version of the framework is your app actually built in?

    – Terry
    Jun 18 '12 at 18:03











  • Under Project Properties on the Application tab, the Target Framework is ".Net Framework 4". Is that sufficient, or do I need to set it somewhere else? Sorry, I'm relatively new to VS (more js experience)

    – njr101
    Jun 18 '12 at 18:14











  • Your code looks correct. Would you please record your AJAX request and the server's reply using WireShark? It would be useful to see these HTTP packets to understand what happens.

    – kol
    Jun 18 '12 at 18:57











  • @kol: Thanks for taking the time to look at this. I have posted the HTTP trace as requested. Maybe you can see something I'm missing.

    – njr101
    Jun 19 '12 at 16:40











  • Possible duplicate: stackoverflow.com/questions/9914782/…

    – mas_oz2k1
    Jul 16 '13 at 7:32
















49















Why does this simple web service refuse to return JSON to the client?



Here is my client code:



        var params = { };
$.ajax({
url: "/Services/SessionServices.asmx/HelloWorld",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
timeout: 10000,
data: JSON.stringify(params),
success: function (response) {
console.log(response);
}
});


And the service:



namespace myproject.frontend.Services
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SessionServices : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
}
}


web.config:



<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>


And the response:



<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>


No matter what I do, the response always comes back as XML. How do I get the web service to return Json?



EDIT:



Here is the Fiddler HTTP trace:



REQUEST
-------
POST http://myproject.local/Services/SessionServices.asmx/HelloWorld HTTP/1.1
Host: myproject.local
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://myproject.local/Pages/Test.aspx
Content-Length: 2
Cookie: ASP.NET_SessionId=5tvpx1ph1uiie2o1c5wzx0bz
Pragma: no-cache
Cache-Control: no-cache

{}

RESPONSE
-------
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 19 Jun 2012 16:33:40 GMT
Content-Length: 96

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>


I have lost count of how many articles I have read now trying to fix this. The instructions are either incomplete or do not solve my issue for some reason.
Some of the more relevant ones include (all without success):




  • ASP.NET web service erroneously returns XML instead of JSON

  • asmx web service returning xml instead of json in .net 4.0

  • http://williamsportwebdeveloper.com/cgi/wp/?p=494

  • http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/

  • http://forums.asp.net/t/1054378.aspx

  • http://jqueryplugins.info/2012/02/asp-net-web-service-returning-xml-instead-of-json/


Plus several other general articles.










share|improve this question

























  • I see the target framework tag set to 4.0, what version of the framework is your app actually built in?

    – Terry
    Jun 18 '12 at 18:03











  • Under Project Properties on the Application tab, the Target Framework is ".Net Framework 4". Is that sufficient, or do I need to set it somewhere else? Sorry, I'm relatively new to VS (more js experience)

    – njr101
    Jun 18 '12 at 18:14











  • Your code looks correct. Would you please record your AJAX request and the server's reply using WireShark? It would be useful to see these HTTP packets to understand what happens.

    – kol
    Jun 18 '12 at 18:57











  • @kol: Thanks for taking the time to look at this. I have posted the HTTP trace as requested. Maybe you can see something I'm missing.

    – njr101
    Jun 19 '12 at 16:40











  • Possible duplicate: stackoverflow.com/questions/9914782/…

    – mas_oz2k1
    Jul 16 '13 at 7:32














49












49








49


19






Why does this simple web service refuse to return JSON to the client?



Here is my client code:



        var params = { };
$.ajax({
url: "/Services/SessionServices.asmx/HelloWorld",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
timeout: 10000,
data: JSON.stringify(params),
success: function (response) {
console.log(response);
}
});


And the service:



namespace myproject.frontend.Services
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SessionServices : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
}
}


web.config:



<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>


And the response:



<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>


No matter what I do, the response always comes back as XML. How do I get the web service to return Json?



EDIT:



Here is the Fiddler HTTP trace:



REQUEST
-------
POST http://myproject.local/Services/SessionServices.asmx/HelloWorld HTTP/1.1
Host: myproject.local
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://myproject.local/Pages/Test.aspx
Content-Length: 2
Cookie: ASP.NET_SessionId=5tvpx1ph1uiie2o1c5wzx0bz
Pragma: no-cache
Cache-Control: no-cache

{}

RESPONSE
-------
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 19 Jun 2012 16:33:40 GMT
Content-Length: 96

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>


I have lost count of how many articles I have read now trying to fix this. The instructions are either incomplete or do not solve my issue for some reason.
Some of the more relevant ones include (all without success):




  • ASP.NET web service erroneously returns XML instead of JSON

  • asmx web service returning xml instead of json in .net 4.0

  • http://williamsportwebdeveloper.com/cgi/wp/?p=494

  • http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/

  • http://forums.asp.net/t/1054378.aspx

  • http://jqueryplugins.info/2012/02/asp-net-web-service-returning-xml-instead-of-json/


Plus several other general articles.










share|improve this question
















Why does this simple web service refuse to return JSON to the client?



Here is my client code:



        var params = { };
$.ajax({
url: "/Services/SessionServices.asmx/HelloWorld",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
timeout: 10000,
data: JSON.stringify(params),
success: function (response) {
console.log(response);
}
});


And the service:



namespace myproject.frontend.Services
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class SessionServices : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
}
}


web.config:



<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>


And the response:



<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>


No matter what I do, the response always comes back as XML. How do I get the web service to return Json?



EDIT:



Here is the Fiddler HTTP trace:



REQUEST
-------
POST http://myproject.local/Services/SessionServices.asmx/HelloWorld HTTP/1.1
Host: myproject.local
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://myproject.local/Pages/Test.aspx
Content-Length: 2
Cookie: ASP.NET_SessionId=5tvpx1ph1uiie2o1c5wzx0bz
Pragma: no-cache
Cache-Control: no-cache

{}

RESPONSE
-------
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 19 Jun 2012 16:33:40 GMT
Content-Length: 96

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>


I have lost count of how many articles I have read now trying to fix this. The instructions are either incomplete or do not solve my issue for some reason.
Some of the more relevant ones include (all without success):




  • ASP.NET web service erroneously returns XML instead of JSON

  • asmx web service returning xml instead of json in .net 4.0

  • http://williamsportwebdeveloper.com/cgi/wp/?p=494

  • http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/

  • http://forums.asp.net/t/1054378.aspx

  • http://jqueryplugins.info/2012/02/asp-net-web-service-returning-xml-instead-of-json/


Plus several other general articles.







c# asp.net json web-services asmx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 11:54









Community

11




11










asked Jun 18 '12 at 17:51









njr101njr101

7,68373353




7,68373353













  • I see the target framework tag set to 4.0, what version of the framework is your app actually built in?

    – Terry
    Jun 18 '12 at 18:03











  • Under Project Properties on the Application tab, the Target Framework is ".Net Framework 4". Is that sufficient, or do I need to set it somewhere else? Sorry, I'm relatively new to VS (more js experience)

    – njr101
    Jun 18 '12 at 18:14











  • Your code looks correct. Would you please record your AJAX request and the server's reply using WireShark? It would be useful to see these HTTP packets to understand what happens.

    – kol
    Jun 18 '12 at 18:57











  • @kol: Thanks for taking the time to look at this. I have posted the HTTP trace as requested. Maybe you can see something I'm missing.

    – njr101
    Jun 19 '12 at 16:40











  • Possible duplicate: stackoverflow.com/questions/9914782/…

    – mas_oz2k1
    Jul 16 '13 at 7:32



















  • I see the target framework tag set to 4.0, what version of the framework is your app actually built in?

    – Terry
    Jun 18 '12 at 18:03











  • Under Project Properties on the Application tab, the Target Framework is ".Net Framework 4". Is that sufficient, or do I need to set it somewhere else? Sorry, I'm relatively new to VS (more js experience)

    – njr101
    Jun 18 '12 at 18:14











  • Your code looks correct. Would you please record your AJAX request and the server's reply using WireShark? It would be useful to see these HTTP packets to understand what happens.

    – kol
    Jun 18 '12 at 18:57











  • @kol: Thanks for taking the time to look at this. I have posted the HTTP trace as requested. Maybe you can see something I'm missing.

    – njr101
    Jun 19 '12 at 16:40











  • Possible duplicate: stackoverflow.com/questions/9914782/…

    – mas_oz2k1
    Jul 16 '13 at 7:32

















I see the target framework tag set to 4.0, what version of the framework is your app actually built in?

– Terry
Jun 18 '12 at 18:03





I see the target framework tag set to 4.0, what version of the framework is your app actually built in?

– Terry
Jun 18 '12 at 18:03













Under Project Properties on the Application tab, the Target Framework is ".Net Framework 4". Is that sufficient, or do I need to set it somewhere else? Sorry, I'm relatively new to VS (more js experience)

– njr101
Jun 18 '12 at 18:14





Under Project Properties on the Application tab, the Target Framework is ".Net Framework 4". Is that sufficient, or do I need to set it somewhere else? Sorry, I'm relatively new to VS (more js experience)

– njr101
Jun 18 '12 at 18:14













Your code looks correct. Would you please record your AJAX request and the server's reply using WireShark? It would be useful to see these HTTP packets to understand what happens.

– kol
Jun 18 '12 at 18:57





Your code looks correct. Would you please record your AJAX request and the server's reply using WireShark? It would be useful to see these HTTP packets to understand what happens.

– kol
Jun 18 '12 at 18:57













@kol: Thanks for taking the time to look at this. I have posted the HTTP trace as requested. Maybe you can see something I'm missing.

– njr101
Jun 19 '12 at 16:40





@kol: Thanks for taking the time to look at this. I have posted the HTTP trace as requested. Maybe you can see something I'm missing.

– njr101
Jun 19 '12 at 16:40













Possible duplicate: stackoverflow.com/questions/9914782/…

– mas_oz2k1
Jul 16 '13 at 7:32





Possible duplicate: stackoverflow.com/questions/9914782/…

– mas_oz2k1
Jul 16 '13 at 7:32












10 Answers
10






active

oldest

votes


















43














Finally figured it out.



The app code is correct as posted. The problem is with the configuration. The correct web.config is:



<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<handlers>
<add name="ScriptHandlerFactory"
verb="*" path="*.asmx"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>


According to the docs, registering the handler should be unnecessary from .NET 4 upwards as it has been moved to the machine.config. For whatever reason, this isn't working for me. But adding the registration to the web.config for my app resolved the problem.



A lot of the articles on this problem instruct to add the handler to the <system.web> section. This does NOT work and causes a whole load of other problems. I tried adding the handler to both sections and this generates a set of other migration errors which completely misdirected my troubleshooting.



In case it helps anyone else, if I had ther same problem again, here is the checklist I would review:




  1. Did you specify type: "POST" in the ajax request?

  2. Did you specify contentType: "application/json; charset=utf-8" in the ajax request?

  3. Did you specify dataType: "json"in the ajax request?

  4. Does your .asmx web service include the [ScriptService] attribute?

  5. Does your web method include the [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    attribute? (My code works even without this attribute, but a lot of articles say that it is required)

  6. Have you added the ScriptHandlerFactory to the web.config file in <system.webServer><handlers>?

  7. Have you removed all handlers from the the web.config file in in <system.web><httpHandlers>?


Hope this helps anyone with the same problem. and thanks to posters for suggestions.






share|improve this answer





















  • 1





    I have been beating my head against the wall for quite a few hours on this and your post has been so very helpful. I have run through all seven steps and my service is still returning xml in the response. I am using ASP.NET 3.5 - any other thoughts? I am also using the jQuery form plugin (not need to explicitly state POST, json, etc.)

    – Daryl
    Nov 20 '12 at 19:47











  • If you open a new question and add a comment here linking to it, I can take a look. Please include a fiddler trace of the request and the response. Include your complete web.config. And also include the asp.net code specifically including attributes. I suggest you test using a similar "Hello World" service as per my example.

    – njr101
    Nov 21 '12 at 8:20











  • This answer helped me a lot, especially the seven steps he mentioned.

    – Ju-chan
    Jan 13 '15 at 0:44











  • i'm using this same in my application but i'm getting an error, can you please suggest me what i'm missing here. XMLHttpRequest cannot load 192.168.200.56/ChatApp.asmx/HelloWorld. Invalid HTTP status code 500

    – Amit Sharma
    Apr 1 '15 at 8:00











  • Adding some more magic for #2, changing contentType to just "application/json" solved my problem.

    – archangel76
    May 6 '15 at 2:29



















24














No success with above solution, here how I resolved it.



put this line into your webservice and rather return type just write the string in response context



this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(serial.Serialize(city));





share|improve this answer





















  • 6





    You did it! This is the best answer. I made my function a "void" return-type and used your code, and JQuery UI autocomplete starts working finally.

    – Dexter
    Jul 3 '13 at 16:42











  • Thank you very much .. It worked for me!

    – S.Mohamed Mahdi Ahmadian zadeh
    Apr 10 '15 at 9:37











  • That's crude and worrying, but brutally effective when the more complicated mucking about doesn't seem to solve my problem. This works.

    – philw
    Aug 21 '16 at 16:26











  • Happy bunny! :D

    – DreamTeK
    Mar 31 '17 at 15:30











  • Dude, your my best savior ever! I've been beating my head against the wall for many hours until I saw your answer, nothing else has helped me. I just love you so much :)

    – Amir Hossein Ahmadi
    May 15 '18 at 12:37





















12














If you want to stay remain with Framework 3.5, you need to make change in code as follows.



[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
}

[WebMethod]
public void HelloWorld() // It's IMP to keep return type void.
{
string strResult = "Hello World";
object objResultD = new { d = strResult }; // To make result similarly like ASP.Net Web Service in JSON form. You can skip if it's not needed in this form.

System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
string strResponse = ser.Serialize(objResultD);

string strCallback = Context.Request.QueryString["callback"]; // Get callback method name. e.g. jQuery17019982320107502116_1378635607531
strResponse = strCallback + "(" + strResponse + ")"; // e.g. jQuery17019982320107502116_1378635607531(....)

Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.AddHeader("content-length", strResponse.Length.ToString());
Context.Response.Flush();

Context.Response.Write(strResponse);
}
}





share|improve this answer


























  • It would be helpful if you added to your answer what you have used and possibly link to the documentation. That way it is a lot easier for someone else to figure out how this exactly works and how they can use it in an other situation.

    – Sumurai8
    Sep 8 '13 at 14:11











  • This answer works good for ASMX + jQuery in Sharepoint 2010 and .NET Framework 3.5. I decided to use standard JavaScriptSerializer instead of reference to NewtonJson assembly. Thanks!

    – opewix
    Aug 15 '15 at 17:39





















6














There is much easier way to return a pure string from web service. I call it CROW function (makes it easy to remember).



  [WebMethod]
public void Test()
{
Context.Response.Output.Write("and that's how it's done");
}


As you can see, return type is "void", but CROW function will still return the value you want.






share|improve this answer
























  • This worked perfectly for me. I changed my string return to a void and used the context.response.output.write to spit out the JSON that was serialized!

    – Josh
    Feb 12 '18 at 2:57



















1














I have a .asmx web service (.NET 4.0) with a method that returns a string. The string is a serialized List like you see in many of the examples. This will return json that is not wrapped in XML. No changes to web.config or need for 3rd party DLLs.



var tmsd = new List<TmsData>();
foreach (DataRow dr in dt.Rows)
{

m_firstname = dr["FirstName"].ToString();
m_lastname = dr["LastName"].ToString();

tmsd.Add(new TmsData() { FirstName = m_firstname, LastName = m_lastname} );

}

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string m_json = serializer.Serialize(tmsd);

return m_json;


The client part that uses the service looks like this:



   $.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: 'json',
url: 'http://localhost:54253/TmsWebService.asmx/GetTombstoneDataJson',
data: "{'ObjectNumber':'105.1996'}",
success: function (data) {
alert(data.d);
},
error: function (a) {
alert(a.responseText);
}
});





share|improve this answer

































    0














    For me it works with this code I got from this post:



    How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?



    [WebInvoke(UriTemplate = "HelloWorld", Method = "GET"), OperationContract]
    public Message HelloWorld()
    {
    string jsonResponse = //Get JSON string here
    return WebOperationContext.Current.CreateTextResponse(jsonResponse, "application/json; charset=utf-8", Encoding.UTF8);
    }





    share|improve this answer


























    • The ScriptService attribute should take care of this automatically. I really don't want to handle all the serialization stuff myself. I have other projects where this code works just fine. But they're on other servers, which makes me think there is some kind of configuration issue.

      – njr101
      Jun 18 '12 at 18:04



















    0














    I have tried all of the above steps ( even the answer), but i was not successful, my system configuration is Windows Server 2012 R2, IIS 8. The following step solved my problem.



    Changed the app pool, that has managed pipeline = classic.






    share|improve this answer































      0














      I know that is really old question but i came to same problem today and I've been searching everywhere to find the answer but with no result. After long research I have found the way to make this work. To return JSON from service you have provide data in request in the correct format, use JSON.stringify() to parse the data before request and don't forget about contentType: "application/json; charset=utf-8", using this should provide expected result.






      share|improve this answer































        0














        Hope this helps, it appears that you still have to send some JSON object in the request, even if the Method you are calling has no parameters.



        var params = {};
        return $http({
        method: 'POST',
        async: false,
        url: 'service.asmx/ParameterlessMethod',
        data: JSON.stringify(params),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json'

        }).then(function (response) {
        var robj = JSON.parse(response.data.d);
        return robj;
        });





        share|improve this answer































          -1














          response = await client.GetAsync(RequestUrl, HttpCompletionOption.ResponseContentRead);
          if (response.IsSuccessStatusCode)
          {
          _data = await response.Content.ReadAsStringAsync();
          try
          {
          XmlDocument _doc = new XmlDocument();
          _doc.LoadXml(_data);
          return Request.CreateResponse(HttpStatusCode.OK, JObject.Parse(_doc.InnerText));
          }
          catch (Exception jex)
          {
          return Request.CreateResponse(HttpStatusCode.BadRequest, jex.Message);
          }
          }
          else
          return Task.FromResult<HttpResponseMessage>(Request.CreateResponse(HttpStatusCode.NotFound)).Result;





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11088294%2fasp-net-asmx-web-service-returning-xml-instead-of-json%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            10 Answers
            10






            active

            oldest

            votes








            10 Answers
            10






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            43














            Finally figured it out.



            The app code is correct as posted. The problem is with the configuration. The correct web.config is:



            <configuration>
            <system.web>
            <compilation debug="true" targetFramework="4.0" />
            </system.web>
            <system.webServer>
            <handlers>
            <add name="ScriptHandlerFactory"
            verb="*" path="*.asmx"
            type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            resourceType="Unspecified" />
            </handlers>
            </system.webServer>
            </configuration>


            According to the docs, registering the handler should be unnecessary from .NET 4 upwards as it has been moved to the machine.config. For whatever reason, this isn't working for me. But adding the registration to the web.config for my app resolved the problem.



            A lot of the articles on this problem instruct to add the handler to the <system.web> section. This does NOT work and causes a whole load of other problems. I tried adding the handler to both sections and this generates a set of other migration errors which completely misdirected my troubleshooting.



            In case it helps anyone else, if I had ther same problem again, here is the checklist I would review:




            1. Did you specify type: "POST" in the ajax request?

            2. Did you specify contentType: "application/json; charset=utf-8" in the ajax request?

            3. Did you specify dataType: "json"in the ajax request?

            4. Does your .asmx web service include the [ScriptService] attribute?

            5. Does your web method include the [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
              attribute? (My code works even without this attribute, but a lot of articles say that it is required)

            6. Have you added the ScriptHandlerFactory to the web.config file in <system.webServer><handlers>?

            7. Have you removed all handlers from the the web.config file in in <system.web><httpHandlers>?


            Hope this helps anyone with the same problem. and thanks to posters for suggestions.






            share|improve this answer





















            • 1





              I have been beating my head against the wall for quite a few hours on this and your post has been so very helpful. I have run through all seven steps and my service is still returning xml in the response. I am using ASP.NET 3.5 - any other thoughts? I am also using the jQuery form plugin (not need to explicitly state POST, json, etc.)

              – Daryl
              Nov 20 '12 at 19:47











            • If you open a new question and add a comment here linking to it, I can take a look. Please include a fiddler trace of the request and the response. Include your complete web.config. And also include the asp.net code specifically including attributes. I suggest you test using a similar "Hello World" service as per my example.

              – njr101
              Nov 21 '12 at 8:20











            • This answer helped me a lot, especially the seven steps he mentioned.

              – Ju-chan
              Jan 13 '15 at 0:44











            • i'm using this same in my application but i'm getting an error, can you please suggest me what i'm missing here. XMLHttpRequest cannot load 192.168.200.56/ChatApp.asmx/HelloWorld. Invalid HTTP status code 500

              – Amit Sharma
              Apr 1 '15 at 8:00











            • Adding some more magic for #2, changing contentType to just "application/json" solved my problem.

              – archangel76
              May 6 '15 at 2:29
















            43














            Finally figured it out.



            The app code is correct as posted. The problem is with the configuration. The correct web.config is:



            <configuration>
            <system.web>
            <compilation debug="true" targetFramework="4.0" />
            </system.web>
            <system.webServer>
            <handlers>
            <add name="ScriptHandlerFactory"
            verb="*" path="*.asmx"
            type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            resourceType="Unspecified" />
            </handlers>
            </system.webServer>
            </configuration>


            According to the docs, registering the handler should be unnecessary from .NET 4 upwards as it has been moved to the machine.config. For whatever reason, this isn't working for me. But adding the registration to the web.config for my app resolved the problem.



            A lot of the articles on this problem instruct to add the handler to the <system.web> section. This does NOT work and causes a whole load of other problems. I tried adding the handler to both sections and this generates a set of other migration errors which completely misdirected my troubleshooting.



            In case it helps anyone else, if I had ther same problem again, here is the checklist I would review:




            1. Did you specify type: "POST" in the ajax request?

            2. Did you specify contentType: "application/json; charset=utf-8" in the ajax request?

            3. Did you specify dataType: "json"in the ajax request?

            4. Does your .asmx web service include the [ScriptService] attribute?

            5. Does your web method include the [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
              attribute? (My code works even without this attribute, but a lot of articles say that it is required)

            6. Have you added the ScriptHandlerFactory to the web.config file in <system.webServer><handlers>?

            7. Have you removed all handlers from the the web.config file in in <system.web><httpHandlers>?


            Hope this helps anyone with the same problem. and thanks to posters for suggestions.






            share|improve this answer





















            • 1





              I have been beating my head against the wall for quite a few hours on this and your post has been so very helpful. I have run through all seven steps and my service is still returning xml in the response. I am using ASP.NET 3.5 - any other thoughts? I am also using the jQuery form plugin (not need to explicitly state POST, json, etc.)

              – Daryl
              Nov 20 '12 at 19:47











            • If you open a new question and add a comment here linking to it, I can take a look. Please include a fiddler trace of the request and the response. Include your complete web.config. And also include the asp.net code specifically including attributes. I suggest you test using a similar "Hello World" service as per my example.

              – njr101
              Nov 21 '12 at 8:20











            • This answer helped me a lot, especially the seven steps he mentioned.

              – Ju-chan
              Jan 13 '15 at 0:44











            • i'm using this same in my application but i'm getting an error, can you please suggest me what i'm missing here. XMLHttpRequest cannot load 192.168.200.56/ChatApp.asmx/HelloWorld. Invalid HTTP status code 500

              – Amit Sharma
              Apr 1 '15 at 8:00











            • Adding some more magic for #2, changing contentType to just "application/json" solved my problem.

              – archangel76
              May 6 '15 at 2:29














            43












            43








            43







            Finally figured it out.



            The app code is correct as posted. The problem is with the configuration. The correct web.config is:



            <configuration>
            <system.web>
            <compilation debug="true" targetFramework="4.0" />
            </system.web>
            <system.webServer>
            <handlers>
            <add name="ScriptHandlerFactory"
            verb="*" path="*.asmx"
            type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            resourceType="Unspecified" />
            </handlers>
            </system.webServer>
            </configuration>


            According to the docs, registering the handler should be unnecessary from .NET 4 upwards as it has been moved to the machine.config. For whatever reason, this isn't working for me. But adding the registration to the web.config for my app resolved the problem.



            A lot of the articles on this problem instruct to add the handler to the <system.web> section. This does NOT work and causes a whole load of other problems. I tried adding the handler to both sections and this generates a set of other migration errors which completely misdirected my troubleshooting.



            In case it helps anyone else, if I had ther same problem again, here is the checklist I would review:




            1. Did you specify type: "POST" in the ajax request?

            2. Did you specify contentType: "application/json; charset=utf-8" in the ajax request?

            3. Did you specify dataType: "json"in the ajax request?

            4. Does your .asmx web service include the [ScriptService] attribute?

            5. Does your web method include the [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
              attribute? (My code works even without this attribute, but a lot of articles say that it is required)

            6. Have you added the ScriptHandlerFactory to the web.config file in <system.webServer><handlers>?

            7. Have you removed all handlers from the the web.config file in in <system.web><httpHandlers>?


            Hope this helps anyone with the same problem. and thanks to posters for suggestions.






            share|improve this answer















            Finally figured it out.



            The app code is correct as posted. The problem is with the configuration. The correct web.config is:



            <configuration>
            <system.web>
            <compilation debug="true" targetFramework="4.0" />
            </system.web>
            <system.webServer>
            <handlers>
            <add name="ScriptHandlerFactory"
            verb="*" path="*.asmx"
            type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            resourceType="Unspecified" />
            </handlers>
            </system.webServer>
            </configuration>


            According to the docs, registering the handler should be unnecessary from .NET 4 upwards as it has been moved to the machine.config. For whatever reason, this isn't working for me. But adding the registration to the web.config for my app resolved the problem.



            A lot of the articles on this problem instruct to add the handler to the <system.web> section. This does NOT work and causes a whole load of other problems. I tried adding the handler to both sections and this generates a set of other migration errors which completely misdirected my troubleshooting.



            In case it helps anyone else, if I had ther same problem again, here is the checklist I would review:




            1. Did you specify type: "POST" in the ajax request?

            2. Did you specify contentType: "application/json; charset=utf-8" in the ajax request?

            3. Did you specify dataType: "json"in the ajax request?

            4. Does your .asmx web service include the [ScriptService] attribute?

            5. Does your web method include the [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
              attribute? (My code works even without this attribute, but a lot of articles say that it is required)

            6. Have you added the ScriptHandlerFactory to the web.config file in <system.webServer><handlers>?

            7. Have you removed all handlers from the the web.config file in in <system.web><httpHandlers>?


            Hope this helps anyone with the same problem. and thanks to posters for suggestions.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 29 '17 at 22:10









            gilly3

            63.9k18117164




            63.9k18117164










            answered Jun 20 '12 at 13:18









            njr101njr101

            7,68373353




            7,68373353








            • 1





              I have been beating my head against the wall for quite a few hours on this and your post has been so very helpful. I have run through all seven steps and my service is still returning xml in the response. I am using ASP.NET 3.5 - any other thoughts? I am also using the jQuery form plugin (not need to explicitly state POST, json, etc.)

              – Daryl
              Nov 20 '12 at 19:47











            • If you open a new question and add a comment here linking to it, I can take a look. Please include a fiddler trace of the request and the response. Include your complete web.config. And also include the asp.net code specifically including attributes. I suggest you test using a similar "Hello World" service as per my example.

              – njr101
              Nov 21 '12 at 8:20











            • This answer helped me a lot, especially the seven steps he mentioned.

              – Ju-chan
              Jan 13 '15 at 0:44











            • i'm using this same in my application but i'm getting an error, can you please suggest me what i'm missing here. XMLHttpRequest cannot load 192.168.200.56/ChatApp.asmx/HelloWorld. Invalid HTTP status code 500

              – Amit Sharma
              Apr 1 '15 at 8:00











            • Adding some more magic for #2, changing contentType to just "application/json" solved my problem.

              – archangel76
              May 6 '15 at 2:29














            • 1





              I have been beating my head against the wall for quite a few hours on this and your post has been so very helpful. I have run through all seven steps and my service is still returning xml in the response. I am using ASP.NET 3.5 - any other thoughts? I am also using the jQuery form plugin (not need to explicitly state POST, json, etc.)

              – Daryl
              Nov 20 '12 at 19:47











            • If you open a new question and add a comment here linking to it, I can take a look. Please include a fiddler trace of the request and the response. Include your complete web.config. And also include the asp.net code specifically including attributes. I suggest you test using a similar "Hello World" service as per my example.

              – njr101
              Nov 21 '12 at 8:20











            • This answer helped me a lot, especially the seven steps he mentioned.

              – Ju-chan
              Jan 13 '15 at 0:44











            • i'm using this same in my application but i'm getting an error, can you please suggest me what i'm missing here. XMLHttpRequest cannot load 192.168.200.56/ChatApp.asmx/HelloWorld. Invalid HTTP status code 500

              – Amit Sharma
              Apr 1 '15 at 8:00











            • Adding some more magic for #2, changing contentType to just "application/json" solved my problem.

              – archangel76
              May 6 '15 at 2:29








            1




            1





            I have been beating my head against the wall for quite a few hours on this and your post has been so very helpful. I have run through all seven steps and my service is still returning xml in the response. I am using ASP.NET 3.5 - any other thoughts? I am also using the jQuery form plugin (not need to explicitly state POST, json, etc.)

            – Daryl
            Nov 20 '12 at 19:47





            I have been beating my head against the wall for quite a few hours on this and your post has been so very helpful. I have run through all seven steps and my service is still returning xml in the response. I am using ASP.NET 3.5 - any other thoughts? I am also using the jQuery form plugin (not need to explicitly state POST, json, etc.)

            – Daryl
            Nov 20 '12 at 19:47













            If you open a new question and add a comment here linking to it, I can take a look. Please include a fiddler trace of the request and the response. Include your complete web.config. And also include the asp.net code specifically including attributes. I suggest you test using a similar "Hello World" service as per my example.

            – njr101
            Nov 21 '12 at 8:20





            If you open a new question and add a comment here linking to it, I can take a look. Please include a fiddler trace of the request and the response. Include your complete web.config. And also include the asp.net code specifically including attributes. I suggest you test using a similar "Hello World" service as per my example.

            – njr101
            Nov 21 '12 at 8:20













            This answer helped me a lot, especially the seven steps he mentioned.

            – Ju-chan
            Jan 13 '15 at 0:44





            This answer helped me a lot, especially the seven steps he mentioned.

            – Ju-chan
            Jan 13 '15 at 0:44













            i'm using this same in my application but i'm getting an error, can you please suggest me what i'm missing here. XMLHttpRequest cannot load 192.168.200.56/ChatApp.asmx/HelloWorld. Invalid HTTP status code 500

            – Amit Sharma
            Apr 1 '15 at 8:00





            i'm using this same in my application but i'm getting an error, can you please suggest me what i'm missing here. XMLHttpRequest cannot load 192.168.200.56/ChatApp.asmx/HelloWorld. Invalid HTTP status code 500

            – Amit Sharma
            Apr 1 '15 at 8:00













            Adding some more magic for #2, changing contentType to just "application/json" solved my problem.

            – archangel76
            May 6 '15 at 2:29





            Adding some more magic for #2, changing contentType to just "application/json" solved my problem.

            – archangel76
            May 6 '15 at 2:29













            24














            No success with above solution, here how I resolved it.



            put this line into your webservice and rather return type just write the string in response context



            this.Context.Response.ContentType = "application/json; charset=utf-8";
            this.Context.Response.Write(serial.Serialize(city));





            share|improve this answer





















            • 6





              You did it! This is the best answer. I made my function a "void" return-type and used your code, and JQuery UI autocomplete starts working finally.

              – Dexter
              Jul 3 '13 at 16:42











            • Thank you very much .. It worked for me!

              – S.Mohamed Mahdi Ahmadian zadeh
              Apr 10 '15 at 9:37











            • That's crude and worrying, but brutally effective when the more complicated mucking about doesn't seem to solve my problem. This works.

              – philw
              Aug 21 '16 at 16:26











            • Happy bunny! :D

              – DreamTeK
              Mar 31 '17 at 15:30











            • Dude, your my best savior ever! I've been beating my head against the wall for many hours until I saw your answer, nothing else has helped me. I just love you so much :)

              – Amir Hossein Ahmadi
              May 15 '18 at 12:37


















            24














            No success with above solution, here how I resolved it.



            put this line into your webservice and rather return type just write the string in response context



            this.Context.Response.ContentType = "application/json; charset=utf-8";
            this.Context.Response.Write(serial.Serialize(city));





            share|improve this answer





















            • 6





              You did it! This is the best answer. I made my function a "void" return-type and used your code, and JQuery UI autocomplete starts working finally.

              – Dexter
              Jul 3 '13 at 16:42











            • Thank you very much .. It worked for me!

              – S.Mohamed Mahdi Ahmadian zadeh
              Apr 10 '15 at 9:37











            • That's crude and worrying, but brutally effective when the more complicated mucking about doesn't seem to solve my problem. This works.

              – philw
              Aug 21 '16 at 16:26











            • Happy bunny! :D

              – DreamTeK
              Mar 31 '17 at 15:30











            • Dude, your my best savior ever! I've been beating my head against the wall for many hours until I saw your answer, nothing else has helped me. I just love you so much :)

              – Amir Hossein Ahmadi
              May 15 '18 at 12:37
















            24












            24








            24







            No success with above solution, here how I resolved it.



            put this line into your webservice and rather return type just write the string in response context



            this.Context.Response.ContentType = "application/json; charset=utf-8";
            this.Context.Response.Write(serial.Serialize(city));





            share|improve this answer















            No success with above solution, here how I resolved it.



            put this line into your webservice and rather return type just write the string in response context



            this.Context.Response.ContentType = "application/json; charset=utf-8";
            this.Context.Response.Write(serial.Serialize(city));






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 3 '12 at 17:41









            Community

            11




            11










            answered Jul 17 '12 at 17:16









            AjayAjay

            24913




            24913








            • 6





              You did it! This is the best answer. I made my function a "void" return-type and used your code, and JQuery UI autocomplete starts working finally.

              – Dexter
              Jul 3 '13 at 16:42











            • Thank you very much .. It worked for me!

              – S.Mohamed Mahdi Ahmadian zadeh
              Apr 10 '15 at 9:37











            • That's crude and worrying, but brutally effective when the more complicated mucking about doesn't seem to solve my problem. This works.

              – philw
              Aug 21 '16 at 16:26











            • Happy bunny! :D

              – DreamTeK
              Mar 31 '17 at 15:30











            • Dude, your my best savior ever! I've been beating my head against the wall for many hours until I saw your answer, nothing else has helped me. I just love you so much :)

              – Amir Hossein Ahmadi
              May 15 '18 at 12:37
















            • 6





              You did it! This is the best answer. I made my function a "void" return-type and used your code, and JQuery UI autocomplete starts working finally.

              – Dexter
              Jul 3 '13 at 16:42











            • Thank you very much .. It worked for me!

              – S.Mohamed Mahdi Ahmadian zadeh
              Apr 10 '15 at 9:37











            • That's crude and worrying, but brutally effective when the more complicated mucking about doesn't seem to solve my problem. This works.

              – philw
              Aug 21 '16 at 16:26











            • Happy bunny! :D

              – DreamTeK
              Mar 31 '17 at 15:30











            • Dude, your my best savior ever! I've been beating my head against the wall for many hours until I saw your answer, nothing else has helped me. I just love you so much :)

              – Amir Hossein Ahmadi
              May 15 '18 at 12:37










            6




            6





            You did it! This is the best answer. I made my function a "void" return-type and used your code, and JQuery UI autocomplete starts working finally.

            – Dexter
            Jul 3 '13 at 16:42





            You did it! This is the best answer. I made my function a "void" return-type and used your code, and JQuery UI autocomplete starts working finally.

            – Dexter
            Jul 3 '13 at 16:42













            Thank you very much .. It worked for me!

            – S.Mohamed Mahdi Ahmadian zadeh
            Apr 10 '15 at 9:37





            Thank you very much .. It worked for me!

            – S.Mohamed Mahdi Ahmadian zadeh
            Apr 10 '15 at 9:37













            That's crude and worrying, but brutally effective when the more complicated mucking about doesn't seem to solve my problem. This works.

            – philw
            Aug 21 '16 at 16:26





            That's crude and worrying, but brutally effective when the more complicated mucking about doesn't seem to solve my problem. This works.

            – philw
            Aug 21 '16 at 16:26













            Happy bunny! :D

            – DreamTeK
            Mar 31 '17 at 15:30





            Happy bunny! :D

            – DreamTeK
            Mar 31 '17 at 15:30













            Dude, your my best savior ever! I've been beating my head against the wall for many hours until I saw your answer, nothing else has helped me. I just love you so much :)

            – Amir Hossein Ahmadi
            May 15 '18 at 12:37







            Dude, your my best savior ever! I've been beating my head against the wall for many hours until I saw your answer, nothing else has helped me. I just love you so much :)

            – Amir Hossein Ahmadi
            May 15 '18 at 12:37













            12














            If you want to stay remain with Framework 3.5, you need to make change in code as follows.



            [WebService(Namespace = "http://tempuri.org/")]
            [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
            // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
            [ScriptService]
            public class WebService : System.Web.Services.WebService
            {
            public WebService()
            {
            }

            [WebMethod]
            public void HelloWorld() // It's IMP to keep return type void.
            {
            string strResult = "Hello World";
            object objResultD = new { d = strResult }; // To make result similarly like ASP.Net Web Service in JSON form. You can skip if it's not needed in this form.

            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            string strResponse = ser.Serialize(objResultD);

            string strCallback = Context.Request.QueryString["callback"]; // Get callback method name. e.g. jQuery17019982320107502116_1378635607531
            strResponse = strCallback + "(" + strResponse + ")"; // e.g. jQuery17019982320107502116_1378635607531(....)

            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Context.Response.AddHeader("content-length", strResponse.Length.ToString());
            Context.Response.Flush();

            Context.Response.Write(strResponse);
            }
            }





            share|improve this answer


























            • It would be helpful if you added to your answer what you have used and possibly link to the documentation. That way it is a lot easier for someone else to figure out how this exactly works and how they can use it in an other situation.

              – Sumurai8
              Sep 8 '13 at 14:11











            • This answer works good for ASMX + jQuery in Sharepoint 2010 and .NET Framework 3.5. I decided to use standard JavaScriptSerializer instead of reference to NewtonJson assembly. Thanks!

              – opewix
              Aug 15 '15 at 17:39


















            12














            If you want to stay remain with Framework 3.5, you need to make change in code as follows.



            [WebService(Namespace = "http://tempuri.org/")]
            [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
            // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
            [ScriptService]
            public class WebService : System.Web.Services.WebService
            {
            public WebService()
            {
            }

            [WebMethod]
            public void HelloWorld() // It's IMP to keep return type void.
            {
            string strResult = "Hello World";
            object objResultD = new { d = strResult }; // To make result similarly like ASP.Net Web Service in JSON form. You can skip if it's not needed in this form.

            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            string strResponse = ser.Serialize(objResultD);

            string strCallback = Context.Request.QueryString["callback"]; // Get callback method name. e.g. jQuery17019982320107502116_1378635607531
            strResponse = strCallback + "(" + strResponse + ")"; // e.g. jQuery17019982320107502116_1378635607531(....)

            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Context.Response.AddHeader("content-length", strResponse.Length.ToString());
            Context.Response.Flush();

            Context.Response.Write(strResponse);
            }
            }





            share|improve this answer


























            • It would be helpful if you added to your answer what you have used and possibly link to the documentation. That way it is a lot easier for someone else to figure out how this exactly works and how they can use it in an other situation.

              – Sumurai8
              Sep 8 '13 at 14:11











            • This answer works good for ASMX + jQuery in Sharepoint 2010 and .NET Framework 3.5. I decided to use standard JavaScriptSerializer instead of reference to NewtonJson assembly. Thanks!

              – opewix
              Aug 15 '15 at 17:39
















            12












            12








            12







            If you want to stay remain with Framework 3.5, you need to make change in code as follows.



            [WebService(Namespace = "http://tempuri.org/")]
            [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
            // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
            [ScriptService]
            public class WebService : System.Web.Services.WebService
            {
            public WebService()
            {
            }

            [WebMethod]
            public void HelloWorld() // It's IMP to keep return type void.
            {
            string strResult = "Hello World";
            object objResultD = new { d = strResult }; // To make result similarly like ASP.Net Web Service in JSON form. You can skip if it's not needed in this form.

            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            string strResponse = ser.Serialize(objResultD);

            string strCallback = Context.Request.QueryString["callback"]; // Get callback method name. e.g. jQuery17019982320107502116_1378635607531
            strResponse = strCallback + "(" + strResponse + ")"; // e.g. jQuery17019982320107502116_1378635607531(....)

            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Context.Response.AddHeader("content-length", strResponse.Length.ToString());
            Context.Response.Flush();

            Context.Response.Write(strResponse);
            }
            }





            share|improve this answer















            If you want to stay remain with Framework 3.5, you need to make change in code as follows.



            [WebService(Namespace = "http://tempuri.org/")]
            [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
            // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
            [ScriptService]
            public class WebService : System.Web.Services.WebService
            {
            public WebService()
            {
            }

            [WebMethod]
            public void HelloWorld() // It's IMP to keep return type void.
            {
            string strResult = "Hello World";
            object objResultD = new { d = strResult }; // To make result similarly like ASP.Net Web Service in JSON form. You can skip if it's not needed in this form.

            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            string strResponse = ser.Serialize(objResultD);

            string strCallback = Context.Request.QueryString["callback"]; // Get callback method name. e.g. jQuery17019982320107502116_1378635607531
            strResponse = strCallback + "(" + strResponse + ")"; // e.g. jQuery17019982320107502116_1378635607531(....)

            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Context.Response.AddHeader("content-length", strResponse.Length.ToString());
            Context.Response.Flush();

            Context.Response.Write(strResponse);
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 8 '13 at 13:51

























            answered Sep 8 '13 at 13:46









            Kalpesh DesaiKalpesh Desai

            12913




            12913













            • It would be helpful if you added to your answer what you have used and possibly link to the documentation. That way it is a lot easier for someone else to figure out how this exactly works and how they can use it in an other situation.

              – Sumurai8
              Sep 8 '13 at 14:11











            • This answer works good for ASMX + jQuery in Sharepoint 2010 and .NET Framework 3.5. I decided to use standard JavaScriptSerializer instead of reference to NewtonJson assembly. Thanks!

              – opewix
              Aug 15 '15 at 17:39





















            • It would be helpful if you added to your answer what you have used and possibly link to the documentation. That way it is a lot easier for someone else to figure out how this exactly works and how they can use it in an other situation.

              – Sumurai8
              Sep 8 '13 at 14:11











            • This answer works good for ASMX + jQuery in Sharepoint 2010 and .NET Framework 3.5. I decided to use standard JavaScriptSerializer instead of reference to NewtonJson assembly. Thanks!

              – opewix
              Aug 15 '15 at 17:39



















            It would be helpful if you added to your answer what you have used and possibly link to the documentation. That way it is a lot easier for someone else to figure out how this exactly works and how they can use it in an other situation.

            – Sumurai8
            Sep 8 '13 at 14:11





            It would be helpful if you added to your answer what you have used and possibly link to the documentation. That way it is a lot easier for someone else to figure out how this exactly works and how they can use it in an other situation.

            – Sumurai8
            Sep 8 '13 at 14:11













            This answer works good for ASMX + jQuery in Sharepoint 2010 and .NET Framework 3.5. I decided to use standard JavaScriptSerializer instead of reference to NewtonJson assembly. Thanks!

            – opewix
            Aug 15 '15 at 17:39







            This answer works good for ASMX + jQuery in Sharepoint 2010 and .NET Framework 3.5. I decided to use standard JavaScriptSerializer instead of reference to NewtonJson assembly. Thanks!

            – opewix
            Aug 15 '15 at 17:39













            6














            There is much easier way to return a pure string from web service. I call it CROW function (makes it easy to remember).



              [WebMethod]
            public void Test()
            {
            Context.Response.Output.Write("and that's how it's done");
            }


            As you can see, return type is "void", but CROW function will still return the value you want.






            share|improve this answer
























            • This worked perfectly for me. I changed my string return to a void and used the context.response.output.write to spit out the JSON that was serialized!

              – Josh
              Feb 12 '18 at 2:57
















            6














            There is much easier way to return a pure string from web service. I call it CROW function (makes it easy to remember).



              [WebMethod]
            public void Test()
            {
            Context.Response.Output.Write("and that's how it's done");
            }


            As you can see, return type is "void", but CROW function will still return the value you want.






            share|improve this answer
























            • This worked perfectly for me. I changed my string return to a void and used the context.response.output.write to spit out the JSON that was serialized!

              – Josh
              Feb 12 '18 at 2:57














            6












            6








            6







            There is much easier way to return a pure string from web service. I call it CROW function (makes it easy to remember).



              [WebMethod]
            public void Test()
            {
            Context.Response.Output.Write("and that's how it's done");
            }


            As you can see, return type is "void", but CROW function will still return the value you want.






            share|improve this answer













            There is much easier way to return a pure string from web service. I call it CROW function (makes it easy to remember).



              [WebMethod]
            public void Test()
            {
            Context.Response.Output.Write("and that's how it's done");
            }


            As you can see, return type is "void", but CROW function will still return the value you want.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 24 '15 at 12:06









            GabrielJGabrielJ

            6111




            6111













            • This worked perfectly for me. I changed my string return to a void and used the context.response.output.write to spit out the JSON that was serialized!

              – Josh
              Feb 12 '18 at 2:57



















            • This worked perfectly for me. I changed my string return to a void and used the context.response.output.write to spit out the JSON that was serialized!

              – Josh
              Feb 12 '18 at 2:57

















            This worked perfectly for me. I changed my string return to a void and used the context.response.output.write to spit out the JSON that was serialized!

            – Josh
            Feb 12 '18 at 2:57





            This worked perfectly for me. I changed my string return to a void and used the context.response.output.write to spit out the JSON that was serialized!

            – Josh
            Feb 12 '18 at 2:57











            1














            I have a .asmx web service (.NET 4.0) with a method that returns a string. The string is a serialized List like you see in many of the examples. This will return json that is not wrapped in XML. No changes to web.config or need for 3rd party DLLs.



            var tmsd = new List<TmsData>();
            foreach (DataRow dr in dt.Rows)
            {

            m_firstname = dr["FirstName"].ToString();
            m_lastname = dr["LastName"].ToString();

            tmsd.Add(new TmsData() { FirstName = m_firstname, LastName = m_lastname} );

            }

            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            string m_json = serializer.Serialize(tmsd);

            return m_json;


            The client part that uses the service looks like this:



               $.ajax({
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            url: 'http://localhost:54253/TmsWebService.asmx/GetTombstoneDataJson',
            data: "{'ObjectNumber':'105.1996'}",
            success: function (data) {
            alert(data.d);
            },
            error: function (a) {
            alert(a.responseText);
            }
            });





            share|improve this answer






























              1














              I have a .asmx web service (.NET 4.0) with a method that returns a string. The string is a serialized List like you see in many of the examples. This will return json that is not wrapped in XML. No changes to web.config or need for 3rd party DLLs.



              var tmsd = new List<TmsData>();
              foreach (DataRow dr in dt.Rows)
              {

              m_firstname = dr["FirstName"].ToString();
              m_lastname = dr["LastName"].ToString();

              tmsd.Add(new TmsData() { FirstName = m_firstname, LastName = m_lastname} );

              }

              var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
              string m_json = serializer.Serialize(tmsd);

              return m_json;


              The client part that uses the service looks like this:



                 $.ajax({
              type: 'POST',
              contentType: "application/json; charset=utf-8",
              dataType: 'json',
              url: 'http://localhost:54253/TmsWebService.asmx/GetTombstoneDataJson',
              data: "{'ObjectNumber':'105.1996'}",
              success: function (data) {
              alert(data.d);
              },
              error: function (a) {
              alert(a.responseText);
              }
              });





              share|improve this answer




























                1












                1








                1







                I have a .asmx web service (.NET 4.0) with a method that returns a string. The string is a serialized List like you see in many of the examples. This will return json that is not wrapped in XML. No changes to web.config or need for 3rd party DLLs.



                var tmsd = new List<TmsData>();
                foreach (DataRow dr in dt.Rows)
                {

                m_firstname = dr["FirstName"].ToString();
                m_lastname = dr["LastName"].ToString();

                tmsd.Add(new TmsData() { FirstName = m_firstname, LastName = m_lastname} );

                }

                var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                string m_json = serializer.Serialize(tmsd);

                return m_json;


                The client part that uses the service looks like this:



                   $.ajax({
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                url: 'http://localhost:54253/TmsWebService.asmx/GetTombstoneDataJson',
                data: "{'ObjectNumber':'105.1996'}",
                success: function (data) {
                alert(data.d);
                },
                error: function (a) {
                alert(a.responseText);
                }
                });





                share|improve this answer















                I have a .asmx web service (.NET 4.0) with a method that returns a string. The string is a serialized List like you see in many of the examples. This will return json that is not wrapped in XML. No changes to web.config or need for 3rd party DLLs.



                var tmsd = new List<TmsData>();
                foreach (DataRow dr in dt.Rows)
                {

                m_firstname = dr["FirstName"].ToString();
                m_lastname = dr["LastName"].ToString();

                tmsd.Add(new TmsData() { FirstName = m_firstname, LastName = m_lastname} );

                }

                var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                string m_json = serializer.Serialize(tmsd);

                return m_json;


                The client part that uses the service looks like this:



                   $.ajax({
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                url: 'http://localhost:54253/TmsWebService.asmx/GetTombstoneDataJson',
                data: "{'ObjectNumber':'105.1996'}",
                success: function (data) {
                alert(data.d);
                },
                error: function (a) {
                alert(a.responseText);
                }
                });






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 8 '13 at 10:12

























                answered May 8 '13 at 9:13









                smoore4smoore4

                2,33921735




                2,33921735























                    0














                    For me it works with this code I got from this post:



                    How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?



                    [WebInvoke(UriTemplate = "HelloWorld", Method = "GET"), OperationContract]
                    public Message HelloWorld()
                    {
                    string jsonResponse = //Get JSON string here
                    return WebOperationContext.Current.CreateTextResponse(jsonResponse, "application/json; charset=utf-8", Encoding.UTF8);
                    }





                    share|improve this answer


























                    • The ScriptService attribute should take care of this automatically. I really don't want to handle all the serialization stuff myself. I have other projects where this code works just fine. But they're on other servers, which makes me think there is some kind of configuration issue.

                      – njr101
                      Jun 18 '12 at 18:04
















                    0














                    For me it works with this code I got from this post:



                    How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?



                    [WebInvoke(UriTemplate = "HelloWorld", Method = "GET"), OperationContract]
                    public Message HelloWorld()
                    {
                    string jsonResponse = //Get JSON string here
                    return WebOperationContext.Current.CreateTextResponse(jsonResponse, "application/json; charset=utf-8", Encoding.UTF8);
                    }





                    share|improve this answer


























                    • The ScriptService attribute should take care of this automatically. I really don't want to handle all the serialization stuff myself. I have other projects where this code works just fine. But they're on other servers, which makes me think there is some kind of configuration issue.

                      – njr101
                      Jun 18 '12 at 18:04














                    0












                    0








                    0







                    For me it works with this code I got from this post:



                    How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?



                    [WebInvoke(UriTemplate = "HelloWorld", Method = "GET"), OperationContract]
                    public Message HelloWorld()
                    {
                    string jsonResponse = //Get JSON string here
                    return WebOperationContext.Current.CreateTextResponse(jsonResponse, "application/json; charset=utf-8", Encoding.UTF8);
                    }





                    share|improve this answer















                    For me it works with this code I got from this post:



                    How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?



                    [WebInvoke(UriTemplate = "HelloWorld", Method = "GET"), OperationContract]
                    public Message HelloWorld()
                    {
                    string jsonResponse = //Get JSON string here
                    return WebOperationContext.Current.CreateTextResponse(jsonResponse, "application/json; charset=utf-8", Encoding.UTF8);
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 23 '17 at 11:54









                    Community

                    11




                    11










                    answered Jun 18 '12 at 18:01









                    j0aqu1nj0aqu1n

                    935614




                    935614













                    • The ScriptService attribute should take care of this automatically. I really don't want to handle all the serialization stuff myself. I have other projects where this code works just fine. But they're on other servers, which makes me think there is some kind of configuration issue.

                      – njr101
                      Jun 18 '12 at 18:04



















                    • The ScriptService attribute should take care of this automatically. I really don't want to handle all the serialization stuff myself. I have other projects where this code works just fine. But they're on other servers, which makes me think there is some kind of configuration issue.

                      – njr101
                      Jun 18 '12 at 18:04

















                    The ScriptService attribute should take care of this automatically. I really don't want to handle all the serialization stuff myself. I have other projects where this code works just fine. But they're on other servers, which makes me think there is some kind of configuration issue.

                    – njr101
                    Jun 18 '12 at 18:04





                    The ScriptService attribute should take care of this automatically. I really don't want to handle all the serialization stuff myself. I have other projects where this code works just fine. But they're on other servers, which makes me think there is some kind of configuration issue.

                    – njr101
                    Jun 18 '12 at 18:04











                    0














                    I have tried all of the above steps ( even the answer), but i was not successful, my system configuration is Windows Server 2012 R2, IIS 8. The following step solved my problem.



                    Changed the app pool, that has managed pipeline = classic.






                    share|improve this answer




























                      0














                      I have tried all of the above steps ( even the answer), but i was not successful, my system configuration is Windows Server 2012 R2, IIS 8. The following step solved my problem.



                      Changed the app pool, that has managed pipeline = classic.






                      share|improve this answer


























                        0












                        0








                        0







                        I have tried all of the above steps ( even the answer), but i was not successful, my system configuration is Windows Server 2012 R2, IIS 8. The following step solved my problem.



                        Changed the app pool, that has managed pipeline = classic.






                        share|improve this answer













                        I have tried all of the above steps ( even the answer), but i was not successful, my system configuration is Windows Server 2012 R2, IIS 8. The following step solved my problem.



                        Changed the app pool, that has managed pipeline = classic.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Apr 9 '14 at 14:39









                        Arindam NayakArindam Nayak

                        6,40442141




                        6,40442141























                            0














                            I know that is really old question but i came to same problem today and I've been searching everywhere to find the answer but with no result. After long research I have found the way to make this work. To return JSON from service you have provide data in request in the correct format, use JSON.stringify() to parse the data before request and don't forget about contentType: "application/json; charset=utf-8", using this should provide expected result.






                            share|improve this answer




























                              0














                              I know that is really old question but i came to same problem today and I've been searching everywhere to find the answer but with no result. After long research I have found the way to make this work. To return JSON from service you have provide data in request in the correct format, use JSON.stringify() to parse the data before request and don't forget about contentType: "application/json; charset=utf-8", using this should provide expected result.






                              share|improve this answer


























                                0












                                0








                                0







                                I know that is really old question but i came to same problem today and I've been searching everywhere to find the answer but with no result. After long research I have found the way to make this work. To return JSON from service you have provide data in request in the correct format, use JSON.stringify() to parse the data before request and don't forget about contentType: "application/json; charset=utf-8", using this should provide expected result.






                                share|improve this answer













                                I know that is really old question but i came to same problem today and I've been searching everywhere to find the answer but with no result. After long research I have found the way to make this work. To return JSON from service you have provide data in request in the correct format, use JSON.stringify() to parse the data before request and don't forget about contentType: "application/json; charset=utf-8", using this should provide expected result.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Mar 23 '17 at 10:47









                                Sebastian SiewieńSebastian Siewień

                                83




                                83























                                    0














                                    Hope this helps, it appears that you still have to send some JSON object in the request, even if the Method you are calling has no parameters.



                                    var params = {};
                                    return $http({
                                    method: 'POST',
                                    async: false,
                                    url: 'service.asmx/ParameterlessMethod',
                                    data: JSON.stringify(params),
                                    contentType: 'application/json; charset=utf-8',
                                    dataType: 'json'

                                    }).then(function (response) {
                                    var robj = JSON.parse(response.data.d);
                                    return robj;
                                    });





                                    share|improve this answer




























                                      0














                                      Hope this helps, it appears that you still have to send some JSON object in the request, even if the Method you are calling has no parameters.



                                      var params = {};
                                      return $http({
                                      method: 'POST',
                                      async: false,
                                      url: 'service.asmx/ParameterlessMethod',
                                      data: JSON.stringify(params),
                                      contentType: 'application/json; charset=utf-8',
                                      dataType: 'json'

                                      }).then(function (response) {
                                      var robj = JSON.parse(response.data.d);
                                      return robj;
                                      });





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        Hope this helps, it appears that you still have to send some JSON object in the request, even if the Method you are calling has no parameters.



                                        var params = {};
                                        return $http({
                                        method: 'POST',
                                        async: false,
                                        url: 'service.asmx/ParameterlessMethod',
                                        data: JSON.stringify(params),
                                        contentType: 'application/json; charset=utf-8',
                                        dataType: 'json'

                                        }).then(function (response) {
                                        var robj = JSON.parse(response.data.d);
                                        return robj;
                                        });





                                        share|improve this answer













                                        Hope this helps, it appears that you still have to send some JSON object in the request, even if the Method you are calling has no parameters.



                                        var params = {};
                                        return $http({
                                        method: 'POST',
                                        async: false,
                                        url: 'service.asmx/ParameterlessMethod',
                                        data: JSON.stringify(params),
                                        contentType: 'application/json; charset=utf-8',
                                        dataType: 'json'

                                        }).then(function (response) {
                                        var robj = JSON.parse(response.data.d);
                                        return robj;
                                        });






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Sep 29 '17 at 9:52









                                        OttoOtto

                                        362




                                        362























                                            -1














                                            response = await client.GetAsync(RequestUrl, HttpCompletionOption.ResponseContentRead);
                                            if (response.IsSuccessStatusCode)
                                            {
                                            _data = await response.Content.ReadAsStringAsync();
                                            try
                                            {
                                            XmlDocument _doc = new XmlDocument();
                                            _doc.LoadXml(_data);
                                            return Request.CreateResponse(HttpStatusCode.OK, JObject.Parse(_doc.InnerText));
                                            }
                                            catch (Exception jex)
                                            {
                                            return Request.CreateResponse(HttpStatusCode.BadRequest, jex.Message);
                                            }
                                            }
                                            else
                                            return Task.FromResult<HttpResponseMessage>(Request.CreateResponse(HttpStatusCode.NotFound)).Result;





                                            share|improve this answer






























                                              -1














                                              response = await client.GetAsync(RequestUrl, HttpCompletionOption.ResponseContentRead);
                                              if (response.IsSuccessStatusCode)
                                              {
                                              _data = await response.Content.ReadAsStringAsync();
                                              try
                                              {
                                              XmlDocument _doc = new XmlDocument();
                                              _doc.LoadXml(_data);
                                              return Request.CreateResponse(HttpStatusCode.OK, JObject.Parse(_doc.InnerText));
                                              }
                                              catch (Exception jex)
                                              {
                                              return Request.CreateResponse(HttpStatusCode.BadRequest, jex.Message);
                                              }
                                              }
                                              else
                                              return Task.FromResult<HttpResponseMessage>(Request.CreateResponse(HttpStatusCode.NotFound)).Result;





                                              share|improve this answer




























                                                -1












                                                -1








                                                -1







                                                response = await client.GetAsync(RequestUrl, HttpCompletionOption.ResponseContentRead);
                                                if (response.IsSuccessStatusCode)
                                                {
                                                _data = await response.Content.ReadAsStringAsync();
                                                try
                                                {
                                                XmlDocument _doc = new XmlDocument();
                                                _doc.LoadXml(_data);
                                                return Request.CreateResponse(HttpStatusCode.OK, JObject.Parse(_doc.InnerText));
                                                }
                                                catch (Exception jex)
                                                {
                                                return Request.CreateResponse(HttpStatusCode.BadRequest, jex.Message);
                                                }
                                                }
                                                else
                                                return Task.FromResult<HttpResponseMessage>(Request.CreateResponse(HttpStatusCode.NotFound)).Result;





                                                share|improve this answer















                                                response = await client.GetAsync(RequestUrl, HttpCompletionOption.ResponseContentRead);
                                                if (response.IsSuccessStatusCode)
                                                {
                                                _data = await response.Content.ReadAsStringAsync();
                                                try
                                                {
                                                XmlDocument _doc = new XmlDocument();
                                                _doc.LoadXml(_data);
                                                return Request.CreateResponse(HttpStatusCode.OK, JObject.Parse(_doc.InnerText));
                                                }
                                                catch (Exception jex)
                                                {
                                                return Request.CreateResponse(HttpStatusCode.BadRequest, jex.Message);
                                                }
                                                }
                                                else
                                                return Task.FromResult<HttpResponseMessage>(Request.CreateResponse(HttpStatusCode.NotFound)).Result;






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Dec 8 '15 at 9:26









                                                Civa

                                                97411029




                                                97411029










                                                answered May 17 '15 at 10:17









                                                Mohamed.AbdoMohamed.Abdo

                                                76669




                                                76669






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11088294%2fasp-net-asmx-web-service-returning-xml-instead-of-json%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Contact image not getting when fetch all contact list from iPhone by CNContact

                                                    count number of partitions of a set with n elements into k subsets

                                                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks