Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
<%@ 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:

VB
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
Posted

1 solution

Dear Robert,

Please note, if your parameter is in string format then use Single Quote like
Ex. data: "{'message':'1'}",

and
In case your parameter is in interger/double format then do not use anything.

Ex. data: "{'message':1}",

Try this:
JavaScript
function GetData() { 
    var msg="1";
    $.ajax({
        url: "Test.aspx/GetData",
        type: "POST",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        data: "{'message':'"+msg+"'}",
        success: function (result) {
           alert(result.d)
        }
    });
}
 
Share this answer
 
Comments
Member 3493606 21-Jun-14 9:02am    
Thanks NitinDhapte, I should have left that in my example, e.g:I did: var data = new Object();data.message = "1"; data:JSON.stringify(data) and that works fine.

I just cannot get it work on an ASMX webservice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900