I'm trying to create a simple AJAX & web service test (using C# .Net 2.0) to return data in JSON format, and I have (I believe) everything I need but I keep running into the same problem over and over again. The response from the web service is always XML. (It always has <?xml version="1.0" encoding="utf-8"?>
as the 1st line).
I've tried the various options that come within the ScriptMethod tag, but nothing makes any difference.
The call works okay, but I get a "parsererror" with the response, which I would expect as it's not valid JSON. I can call the web service method within the browser and get the return value I expect, just in XML format. Fiddler also shows that the request and response are as I've found.
There are already several questions on here about the exact same issue, but none of them have given me a working answer.
Please ask questions if you need more information.
C#
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
namespace jQueryWebServiceTest
{
/// <summary>
/// This is a simple web service to test calling from javascript using AJAX, and getting a response.
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string SayHello()
{
// I've tried both this...
var response = new { value = "Hello" };
JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(response);
// and this...
return "Hello";
}
}
}
Javascript
$(function () {
var data = JSON.stringify({ Name: "John" }); // This is not used - part of other testing.
data = "{}";
$.ajax({
type: "POST",
data: data,
url: "TestService.asmx/SayHello",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (error, status) {
console.log("status = " + status);
console.log(error);
}
});
});
-
Take a look at this answer: stackoverflow.com/a/663860/701062Gary.S– Gary.S2012年02月27日 17:18:11 +00:00Commented Feb 27, 2012 at 17:18
-
Thanks Gary. I've already seen that one but it's not relevant as I'm using .Net 2.0 (I'll amend the question to reflect that). Thanks all the same.Reinstate Monica Cellio– Reinstate Monica Cellio2012年02月27日 17:20:04 +00:00Commented Feb 27, 2012 at 17:20
-
@Archer Actually you'd have to be using .Net 3.5. The ScriptMethod attribute wasn't even introduced until that version.JamieSee– JamieSee2012年02月27日 17:31:02 +00:00Commented Feb 27, 2012 at 17:31
-
@JamieSee you can use the AJAX extensions in .Net 2 (and I think .Net 1 actually), as long as you have a reference to the relevant library (which I have or it wouldn't compile or run.) Thanks though.Reinstate Monica Cellio– Reinstate Monica Cellio2012年02月27日 17:33:34 +00:00Commented Feb 27, 2012 at 17:33
-
1Perhaps this is a problem with the AJAX Extensions? If you try this as 3.5 without the old extensions library does it work? Also, I found a thread which may help: forums.asp.net/t/1054378.aspxJamieSee– JamieSee2012年02月27日 18:46:37 +00:00Commented Feb 27, 2012 at 18:46
1 Answer 1
I managed to get it working by adding the following 2 blocks to the web.config...
In the configuration
block inside system.web
...
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
and anywhere else, inside system.web
...
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>