I am trying to call my Web API web service from my ASP.net code behind.
Using the WebMethod function:
[WebMethod]
public static string checkQuery(string sql)
{
string encryptingIT = new AES().Encrypt(sql);
string result = q(encryptingIT);
return result;
}
public async Task<string> q(string encryptingIT)
{
var client = new HttpClient();
var content = new StringContent(JsonConvert.SerializeObject(new Product { query = encryptingIT, empImg = false }));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync("http://dev-zzzz/newWS/theQ", content);
var value = await response.Content.ReadAsStringAsync();
return value;
}
However, I have an error on line:
q(encryptingIT);
Which states:
Error 16 An object reference is required for the non-static field, method, or property 'WebApi.App._default.q(string)'
I tried putting the HttpClient into the same checkQuery function but it seems that I am not allowed to call the function from a asp button click when I do that.
I have used the web service mostly with jQuery Ajax like this:
$.support.cors = true;
$.ajax({
type: "POST",
crossDomain: true,
url: "http://dev-zzzz/newWS/theQ",
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/json");
},
data: JSON.stringify({
theID: "2135648792",
empImg: "false"
}),
dataType: "json",
success: function (msg) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
And that works just fine.
What can I do to mimic that AJAX in my code behind WebMethod function?