Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I couldn't be able to figure out the problem in the below case. I have a webmethod and calling the same using jquery ajax call. Below is the code.

C#
[WebMethod(EnableSession = true)]
    [ScriptMethod()]
    public void SaveSearch(cnt strProp)
    {
    }


where cnt is the class.

When i calling this using ajax call like.
JavaScript
var strArray = {
                 cntName : "1",
                 cntEmail : "2",
                 cntMsg : "3"
            }

$.ajax({
               url: '/getproductsservice.asmx/SaveSearch',
               method: 'POST',
               data:  { strProp : strArray},
               contentType: 'application/json; charset=utf-8',
               success: function (data) {
                   var showMsg = ''
                   alert(showMsg);
               },
               error: function (data) {
                   alert('Save Search Failed');
               }
           });


When running this, i got the exception stating that "SaveSearch Service method name is not valid".

But if I replace the parameter with primitive data type like string or int it is working well. Problem is with the object types (list or object etc).

And if I use the primitive type, it is working only if the ajax content type is "content/xml" not working with "content/json". If I use the json type getting another exception of "An attempt was made to call the method \u0027SaveSearch\u0027 using a GET request, which
is not allowed."

Kindly help to resolve this.
Posted
Comments
Kornfeld Eliyahu Peter 2-Dec-15 1:56am    
The JSON data you send does not fit the type cnt on the server side...You may show us how that one defined!
Kumarbs 2-Dec-15 2:21am    
Following is the json data:
"{"cntName":"1","cntEmail":"2","cntMsg":"3"}"
Kornfeld Eliyahu Peter 2-Dec-15 2:23am    
I didn't ask for the JSON data, but for the class definition that should receive it!
Kumarbs 2-Dec-15 2:34am    
public class cnt
{
public string cntName;
public string cntEmail;
public string cntMsg;
}
Kornfeld Eliyahu Peter 2-Dec-15 2:42am    
Chnage it like this:
public class cnt
{
public string cntName{get;set;}
public string cntEmail{get;set;}
public string cntMsg{get;set;}
}

1 solution

JavaScript
var strArray = {
                 cntName : "1",
                 cntEmail : "2",
                 cntMsg : "3"
            }
var input = { 'strProp' : strArray };
$.ajax({
               url: '/getproductsservice.asmx/SaveSearch',
               method: 'POST',
               data:  JSON.stringify(input),
               contentType: 'application/json; charset=utf-8',
               success: function (data) {
                   var showMsg = ''
                   alert(showMsg);
               },
               error: function (data) {
                   alert('Save Search Failed');
               }
           });
 
Share this answer
 
Comments
Kumarbs 2-Dec-15 2:33am    
Thank you for your solution. But, unfortunately it is not working. Getting the exception "{"Message":"An attempt was made to call the method \u0027SaveSearch\u0027 using a GET request, which
is not allowed.","StackTrace":" at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData
methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall
(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"
}"
I decorated the web method with [ScriptMethod(UseHttpGet=true)] too after getting the above exception. But still not working.

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