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:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.Web.Script.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string HelloWorld() {
return "Hello World";
}
}
Here is my Test.htm source:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
var req = new XMLHttpRequest();
function Roll()
{
if (req.readyState == 4)
{
alert(req.responseText);
alert("exiting");
}
}
function Test()
{
alert("starting");
req.open("POST", "http://localhost:52120/JSON_Test_4/WebService.asmx/HelloWorld", true);
req.onreadystatechange = Roll;
req.send(null);
}
</script>
</head>
<body>
<div id="test">Bleh</div>
<button onclick="javascript:Test()">TEST IT</button>
</body>
</html>
Why is the webservice only returning XML? How do I get it to return JSON as advertised?
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
This posting is provided "AS IS" with no warranties, and confers no rights.
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>
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.
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.
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.
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!!!!
johnsm
Member
6 Points
5 Posts
WebService with ResponseFormat.Json won't return JSON, only XML
Dec 14, 2006 03:21 AM|LINK
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:
using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using Microsoft.Web.Script.Services; /// <summary> /// Summary description for WebService /// </summary> [ScriptService] public class WebService : System.Web.Services.WebService { public WebService () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] [ScriptMethod(ResponseFormat=ResponseFormat.Json)] public string HelloWorld() { return "Hello World"; } }Here is my Test.htm source:
Why is the webservice only returning XML? How do I get it to return JSON as advertised?
JSON serialization web service Xml asmx
fomine
Member
90 Points
19 Posts
Re: WebService with ResponseFormat.Json won't return JSON, only XML
Dec 14, 2006 06:35 AM|LINK
I am not sure, did you try to specify content-type in request?
Content-Type: application/json
fomine
Member
90 Points
19 Posts
Re: WebService with ResponseFormat.Json won't return JSON, only XML
Dec 14, 2006 06:41 AM|LINK
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":[]}
maira.wenzel
Member
511 Points
111 Posts
Microsoft
Re: WebService with ResponseFormat.Json won't return JSON, only XML
Dec 14, 2006 06:09 PM|LINK
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
johnsm
Member
6 Points
5 Posts
Re: WebService with ResponseFormat.Json won't return JSON, only XML
Dec 15, 2006 01:23 AM|LINK
This is the correct answer! [:D]
With this, it is possible to merge Mochikit and Atlas into pure awesome.
Thanks all for the help!
ajax beta 2 serialization web service JSO Mochikit
stevegourley
Member
16 Points
17 Posts
Re: WebService with ResponseFormat.Json won't return JSON, only XML
Sep 25, 2007 07:11 PM|LINK
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>
panvega
Member
2 Points
1 Post
Re: WebService with ResponseFormat.Json won't return JSON, only XML
Apr 07, 2008 05:47 PM|LINK
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?
JSON WebMethod ResponseFormat setRequestHeader responseText
jonintc
Member
4 Points
2 Posts
Re: WebService with ResponseFormat.Json won't return JSON, only XML
May 05, 2008 09:50 PM|LINK
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.
kxag
Member
2 Points
1 Post
Re: WebService with ResponseFormat.Json won't return JSON, only XML
Nov 12, 2008 12:32 AM|LINK
jquery ajax web service json xml returntype