Yonathanb
Yonathanb

Reputation: 57

Calling a C# webmethod from Ajax

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

Upvotes: 0

Views: 85

Answers (1)

Akshey Bhat
Akshey Bhat

Reputation: 8545

$.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.

Upvotes: 1

Related Questions