0

I'm trying to call a server side function from ajax but keep getting this error

Invalid web service call, missing value for parameter: \u0027name\u0027.

here is the js code

var obj = { name: name, company: company, country: country, email: email, msg: Msg }
var json = JSON.stringify(obj);
$.ajax({
    method: "GET",
    url: "ContactUs.aspx/SendEmail",
    contentType: "application/json; charset=UTF-8",
    data: json,
    dataType:"json",
    success: function (data) {
        var a = 3;
    },
    error:function(a,b){
        var a = 43;
    }
})

and here is the c# webmethod

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string SendEmail(string name, string company, string country, string email, string msg)
{
    return string.Empty;
}

the names are identical between the js and c# vars and all of my vars have values.

thanks in advance

6
  • You are doing a POST but the method looks like its a GET? Commented Mar 16, 2017 at 17:13
  • No. that was me tring to fix the problem, it should be GET Commented Mar 16, 2017 at 17:15
  • 1
    Check this answer. Pass the entire object. Commented Mar 16, 2017 at 17:19
  • I don't think you need to stringify obj Commented Mar 16, 2017 at 17:21
  • I think a webmethod must be POST. here is a quick example of one aspsnippets.com/Articles/… Commented Mar 16, 2017 at 17:30

1 Answer 1

1
$.ajax({
    method: "GET",
    url: "ContactUs.aspx/SendEmail?name=" + name + "&company=" + company + "&country=" + "&email=" +email + "&msg=" + Msg,
    dataType: "application/json",
    success: function (data) {
        var a = 3;
    },
    error:function(a,b){
        var a = 43;
    }
}) ;

Since its a GET type of request you need to pass required values for webmethod parameter in query string. you cant use data parameter for this as GET type of request has no payload.

Sign up to request clarification or add additional context in comments.

1 Comment

I think Akshey might be correct. I would recommend using POST instead of GET, if possible, to avoid this limitation. For one thing, some web servers will not handle very long URLs correctly, so if you have a large amount of data in the URL, it is possible that the web server might truncate it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.