Answered by:
WebService with ResponseFormat.Json won't return JSON, only XML

Question
-
User-575693569 posted
I can't get my ASP.NET AJAX webservice to return JSON, it always returns XML. The docs say that it should return JSON automatically -- how do I enable this?
Here is my WebService.cs source:
1 using System; 2 using System.Web; 3 using System.Collections; 4 using System.Web.Services; 5 using System.Web.Services.Protocols; 6 using Microsoft.Web.Script.Services; 7 8 /// <summary> 9 /// Summary description for WebService 10 /// </summary> 11 12 [ScriptService] 13 public class WebService : System.Web.Services.WebService { 14 15 public WebService () { 16 17 //Uncomment the following line if using designed components 18 //InitializeComponent(); 19 } 20 21 [WebMethod] 22 [ScriptMethod(ResponseFormat=ResponseFormat.Json)] 23 public string HelloWorld() { 24 return "Hello World"; 25 } 26 27 } 28 29
Here is my Test.htm source:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" > 3 <head> 4 <title>Untitled Page</title> 5 <script language="javascript" type="text/javascript"> 6 7 var req = new XMLHttpRequest(); 8 9 function Roll() 10 { 11 if (req.readyState == 4) 12 { 13 alert(req.responseText); 14 alert("exiting"); 15 } 16 } 17 18 function Test() 19 { 20 alert("starting"); 21 req.open("POST", "http://localhost:52120/JSON_Test_4/WebService.asmx/HelloWorld", true); 22 req.onreadystatechange = Roll; 23 req.send(null); 24 } 25 26 27 </script> 28 </head> 29 <body> 30 <div id="test">Bleh</div> 31 <button onclick="javascript:Test()">TEST IT</button> 32 </body> 33 </html> 34
Why is the webservice only returning XML? How do I get it to return JSON as advertised?
Wednesday, December 13, 2006 11:21 PM
Answers
-
User-1220773483 posted
Here the request generated by AJAX.NET Beta 2 proxy, see 'js' before method name:
POST /Source/service.asmx/js/UpdateChat HTTP/1.1
Host: localhost:1123
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Content-Type: application/json
Content-Length: 50
Cookie: ASP.NET_SessionId=qpspnn45mgxzlp20xdj0dvng; .SLCVISITOR=200529225
Pragma: no-cache
Cache-Control: no-cache{"a":246144119,"v":200529225,"n":18,"z":-1,"m":[]}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 14, 2006 2:41 AM
All replies
-
User-1220773483 posted
I am not sure, did you try to specify content-type in request?
Content-Type: application/json
Thursday, December 14, 2006 2:35 AM -
User-1220773483 posted
Here the request generated by AJAX.NET Beta 2 proxy, see 'js' before method name:
POST /Source/service.asmx/js/UpdateChat HTTP/1.1
Host: localhost:1123
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Content-Type: application/json
Content-Length: 50
Cookie: ASP.NET_SessionId=qpspnn45mgxzlp20xdj0dvng; .SLCVISITOR=200529225
Pragma: no-cache
Cache-Control: no-cache{"a":246144119,"v":200529225,"n":18,"z":-1,"m":[]}
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 14, 2006 2:41 AM -
User933528419 posted
Hi,
I believe that this is true when you are using the Ajax architecture to invoke the Web service method.
The way you're doing, you're using the XMLHttpRequest object directly.You should add a ScriptManager with a ServiceReference inside pointing to your Web service and then invoking your method in the javascript function like this:
WebService.HelloWorld(OnSucceeded);The OnSucceeded is your succeeded callback function that you also have to code. For example:
function OnSucceeded(result)
{
alert(result);
}Hope this helps,
Maíra
Thursday, December 14, 2006 2:09 PM -
User-575693569 posted
This is the correct answer! [:D]
With this, it is possible to merge Mochikit and Atlas into pure awesome.
Thanks all for the help!
Thursday, December 14, 2006 9:23 PM -
User-303649323 posted
I'm still having this problem but I wrote my web service in .NET and it won't stop wrapping the JSON in xml. How do you get around this? Do I need to make my return type not a string? You're saying its the way you call the service? i'm using JSONscriptRequest from a .js file I found online for using dynamic script tags. I'm trying to get around the cross domain request security issue.
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://mapserv.utah.gov/WSUTSGID_Geolocator">ws_results({"MatchAddress":"326 E South Temple St, 84111","Geocoder":"U024.GC_StatewideStreets","Score":81,"UTM_X":425585.8,"UTM_Y":4513511.97,"LONG_X":-111.8817402,"LAT_Y":40.7692131})</string>
Tuesday, September 25, 2007 3:11 PM -
User-1595463325 posted
I have a similar problem. I want to consum the json ws method from a simple html, javascript page without any asp.net. Should I serialize my List-generic List to string format or return this GnericList with any manual serialization?
In JavaScript I used, nut it´s not possible to access the object in the client site.
.....
req.open("POST", "http://domain/service.asmx/getProjectsJson",true);
req.setRequestHeader("Content-Type","application/json");
req.onreadystatechange = Roll;
req.send(null);.....
//var object= eval('(' + req.responseText + ')');
When accessing the req.getAllResponseHeaders(); the content-type shows text/xml instead of application/json. How can I solve this problem?
Monday, April 7, 2008 1:47 PM -
User145862239 posted
I was running into the same issue. I modified my .NET 2.0 Web Service with AJAX Extensions 1.0 to include the ScriptHandlerFactory, [ScriptService] attribute and [ScriptMethod(ResponseFormat=ResponseFormat.Json)] attribute and property.
You must specify the content-type as application/json when making the ajax call. Fiddler will show the response as application/json.
xmlhttp.onreadystatechange=state_Change; xmlhttp.open("POST",url,true); xmlhttp.setRequestHeader("Content-Type","application/json"); xmlhttp.send(null);
On a side note, I have been unable to use <form method="POST" enctype="application/json"> to get my asmx page to return json. My original request is always application/x-www-form-urlencoded.
Monday, May 5, 2008 5:50 PM -
User-2011131586 posted
I was trying to call a webservice method through the jquery ajax metod and guess what: I was always getting an xml response
$.ajax(
{
type: "POST",url:
http://localhost:50080/ElearningWebApplication/ChatWebService.asmx/InserMessage, data: "{}",contentType:
"application/json; charset=utf-8", dataType: "json",success: GetMessagesBack,
fail: Failure
}
);
I included the correct attributes to both the service and the method
[WebService(Namespace = "http://www.action.gr/")][
WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class ChatWebService : System.Web.Services.WebService{
[WebMethod][
ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string InsertMessage(string text, int roomID, int userID, string color, int toUserID){
.....
}
}
After 3 hours of searching everywhere i realized that I had to add the following to the web config the following besides the fact i am not using the scriptmanager or what ever its name is!!!!!! But it is so obvious...not!!!!
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
Tuesday, November 11, 2008 8:32 PM