Javascript 向ASP.NETWebAPI发送ajax GET请求
我使用以下ajax脚本Javascript 向ASP.NETWebAPI发送ajax GET请求,javascript,jquery,asp.net,ajax,Javascript,Jquery,Asp.net,Ajax,我使用以下ajax脚本 $.ajax({ dataType: 'json', url: url, data: tuDispId, type: "GET", success: function (data) { bindData(data); $("#alert-placeholder").empty();
$.ajax({
dataType: 'json',
url: url,
data: tuDispId,
type: "GET",
success: function (data) {
bindData(data);
$("#alert-placeholder").empty();
$('#alert-placeholder').removeClass('alert alert-danger');
},
error: function (xhr, textStatus, errorThrown) {
$('#alert-placeholder').addClass('alert alert-danger');
$('#alert-placeholder').html(errorThrown);
}
});
方法之前Web API中的属性路由
[Route("api/tudisp/Edit/{tuDispId}")]
public IHttpActionResult Edit(int tuDispId)
{
}
来自ajax的请求
http://localhost:xxxxx/api/tudisp/Edit/?179
如何强制ajax不通过id参数生成符号“?”。最简单的方法是更改ajax选项的url属性
$.ajax({
dataType: 'json',
url: "http://localhost:xxxxx/api/tudisp/Edit/" + tuDispId,
type: "GET",
success: function (data) {
bindData(data);
$("#alert-placeholder").empty();
$('#alert-placeholder').removeClass('alert alert-danger');
},
error: function (xhr, textStatus, errorThrown) {
$('#alert-placeholder').addClass('alert alert-danger');
$('#alert-placeholder').html(errorThrown);
}
});
GET参数作为查询字符串自动附加到Url,如果这是应用程序所期望的,那么这是很好的,但在使用路由时就不是了
但是,如果您想修改现有的Ajax请求,可以使用预过滤。此示例修改ajax调用的Url,用数据对象中的给定值替换{variable}
$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
options.data = ""; // this removes the querystring
for (key in originalOptions.data) {
options.url = options.url.replace("{" + key + "}", originalOptions.data[key]);
}
});
$.ajax({
url: "http://localhost:xxxxx/api/tudisp/Edit/{tuDispId}",
data: {
"tuDispId": 179
}
});
如果你想使用类似的东西,那么我强烈建议你花点时间让它更健壮,但这是一个你可以做你想做的事情的例子。因为方法是Get,所以你必须在你的请求下编写
数据参数:数据:{id:tuDispId}
url:url+“/”+tuDispId,
这个解决方案我已经测试过了,我想知道是否可以使用部分数据。谢谢你走了-我添加了一些可能是你想要的东西。实际上我很喜欢它,将来我自己可能会用它