【发布时间】:2016-02-19 18:49:37
【问题描述】:
我正在尝试从背后的 ASP.net 代码调用我的 Web API Web 服务。
使用 WebMethod 函数:
[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;
}
但是,我在线出错:
q(加密IT);
其中规定:
错误 16 非静态字段、方法或属性“WebApi.App._default.q(string)”需要对象引用
我尝试将 HttpClient 放入同一个 checkQuery 函数,但似乎不允许我在执行此操作时通过单击 asp 按钮调用该函数。
我主要通过 jQuery Ajax 使用 Web 服务,如下所示:
$.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) {
}
});
而且效果很好。
我可以做些什么来在我的WebMethod 函数背后的代码中模仿AJAX?
【问题讨论】:
-
错误很明显,你不能在没有引用类实例的情况下从静态调用实例函数,只需将第二个也设置为静态就可以了,如果您必须将该函数保留为实例,则将代码移动到静态函数并从实例调用它
标签: c# jquery asp.net asp.net-web-api asp.net-web-api2