[英]How to receive data in Json format from web method using JQuery Ajax Call?
I have a text box and a button next to it. 我旁边有一个文本框和一个按钮。 I want to send the content of textbox that is a EmailId through Jquery ajax call to webmethod and get back the data associated with that EmailId The WebMethod returns a group of records in JSON format as:
我想通过对Webmethod的Jquery ajax调用将文本框的内容发送给EmailId,并获取与该EmailId关联的数据。WebMethod以JSON格式返回一组记录,如下所示:
[HttpGet]
[Route("api/Drive/GetDriveByEmail/{email}")]
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public IEnumerable<object> GetDriveByEmail(string email)
{
using (var db1 = new RigilCares2Context())
{
var results = from d in db1.Drives
where d.email == email
select d;
return results;
}
}
The html file with jquery call is as: 带有jquery调用的html文件为:
<div>
<h2>Enter Your Email Address </h2>
<input type="text" id="useremail" size="5" />
<input type="button" value="Search" onclick="findByEmail();" />
<p id="drive" />
</div>
<h2>Here displays returned data from web api</h2>
<div id="divResult">
The script is as: 脚本为:
function findByEmail() {
var email = $('#useremail').val();
$.ajax({
type: "POST",
dataType: "json",
url: "/api/drive" + '/' + email,
data: source,
success: function (data) {
getdrivelist();
},
error: function (x, y, z) {
var strResult = "<table><th>Error Message</th>";
strResult += "<tr><td> " + x.responseText + " </td></tr>"
strResult += "</table>";
$("#divResult").html(strResult);
}
});
}
I dont know how to define the data property of this call.Can someone please help me to write the complete jquery function to receive a group of records in json format from that webmethod. 我不知道如何定义此调用的data属性。有人可以帮我编写完整的jquery函数,以从该webmethod接收json格式的一组记录。
Also,added a route in webapiconfig file as: 另外,在webapiconfig文件中添加了一条路由,如下所示:
config.Routes.MapHttpRoute(
name: "actionApiByEmail",
routeTemplate: "api/{controller}/{action}/{email}",
defaults: new { action = "GetDriveByEmail", Id = RouteParameter.Optional }
);
Hope this will help. 希望这会有所帮助。
function findByEmail() {
var email = $('#useremail').val();
$.ajax({
type: "Get",
dataType: "json",
url: "/api/drive/GetDriveByEmail",
contentType: 'application/json',
data: JSON.stringify({email:email}),
success: function (data) {
getdrivelist();
},
error: function (x, y, z) {
var strResult = "<table><th>Error Message</th>";
strResult += "<tr><td> " + x.responseText + " </td></tr>"
strResult += "</table>";
$("#divResult").html(strResult);
}
});
} }
Given that your url already includes the email you don't need any data at all, as this would create sth. 假设您的网址已经包含了电子邮件,那么您根本不需要任何数据,因为这会创建一些东西。 like
/api/drive?email=foo@bar.net
(for GET), which does not fit your route. 例如
/api/drive?email=foo@bar.net
(用于GET),它不适合您的路线。 However, url: "/api/drive" + '/' + email
will set email
as action, so what you need to do is url: "/api/drive/GetDriveByEmail/" + email
然而,
url: "/api/drive" + '/' + email
将设置email
的行动,所以你需要做的是url: "/api/drive/GetDriveByEmail/" + email
I think there are some errors in your route config, usually, you do not need change the global default route, just leave it as: 我认为您的路由配置中存在一些错误,通常,您不需要更改全局默认路由,只需将其保留为:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
In your controller, you can override the route like this: 在您的控制器中,您可以像这样覆盖路由:
public class DriveController : ApiController {
[Route("~/api/drive/{email}")]
public IEnumerable<object> GetDriveByEmail(string email) {
}
}
Then you can do GET request to: api/drive/someone@localhost
, the jQuery ajax request should like: 然后,您可以向以下位置执行GET请求:
api/drive/someone@localhost
,jQuery ajax请求应如下:
function findByEmail() {
var email = $('#useremail').val();
// use promise style ajax call is recommended
$.ajax({
url: 'api/drive/' + email,
method: 'GET',
dataType: 'json'
})
.done(function(data) {
// call back when success
})
.fail(function(err) {
// error call back;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.