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

Yonathanb
  • 57
  • 10
  • You are doing a POST but the method looks like its a GET? – Neil Mar 16 '17 at 17:13
  • No. that was me tring to fix the problem, it should be GET – Yonathanb Mar 16 '17 at 17:15
  • 1
    [Check this answer](http://stackoverflow.com/questions/18078957/pass-a-user-defined-object-to-asp-net-webmethod-from-jquery-using-json/18079544#18079544). Pass the entire object. – Equalsk Mar 16 '17 at 17:19
  • I don't think you need to stringify obj – Neil Mar 16 '17 at 17:21
  • I think a webmethod must be POST. here is a quick example of one https://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx – Bryan Dellinger Mar 16 '17 at 17:30
  • orignially o used post but it kept giving me this error: An attempt was made to call the method \u0027SendEmail\u0027 using a GET request, which is not allowed. so i switched to get – Yonathanb Mar 16 '17 at 17:40

1 Answers1

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.

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
  • 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. – Joe Irby Mar 16 '17 at 21:14