VS2010
ASP.Net 4.0
Solution: Test.sln with 1 project
Web Application
Project: Test
Test.aspx (client code)
Test.aspx.cs (web method)
This works:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BlendSpecAlerts.aspx.cs" Inherits="ADS.BlendOptimizer.BlendSpecAlerts" %>
<!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 runat="server">
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/json.js"></script>
<script type="text/javascript">
function GetData() {
var data = new Object();
data.Message= "1";
$.ajax({
url: "Test.aspx/GetData",
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(data),
success: function (result) {
alert(result.d)
}
})
}
</script>
<input type="button" önclick="Getdata()"/>
</body>
</html>
Test.aspx.cs
[WebMethod()]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetData(string message)
{
return "I returned your " + message
This does NOT work:
Now, when I create a new project within Test.sln MyWebService with Test.ASMX service and include the same web method like below:
In the jquery call:
url: "http://localhost/MyWebService/Test.aspx/GetData"
[WebService(Namespace = "http://blabla")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Test: System.Web.Services.WebService
{
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string GetData(string message)
{
return "I returned your " + message
}
Result:
Error in: http://localhost/Test/MyWebService/Test.asmx/Test
error: undefined
type: ajaxError
readystate: 4
status: 500
I can access the .asmx directly ok by pasting in http://localhost/test/test.asmx/Test
and I get a result.
For many hours I have looked for solutions, (cross domain issues (to which I follow the recommendations to setup the web service to allow a cross domain request), etc, web.config settings.. )
I am at my wits end here. I feel I need someone next to me to show me what am I doing wrong.
Please don't provide some link that will have the magical solution, I have been there.
Why is this not working!?
Thanks,
Robert