我刚开始使用.net,我可以提供完整的webapi端点网址来进行AJAX调用或提供相对网址。在使用相对URL时,客户端项目如何知道在哪里找到webapi端点?
客户端AJAX当前通话:
$.ajax({
url: localhost:3227/api/form/post,
contentType: "application/json; charset=UTF-8",
type: "POST",
dataType: "json",
data: JSON.stringify(formData),
success: function (resp) {
console.log("Updated successfully!"); },
error: function (request, status, error) {
console.log(request.responseText + ' Error:' + error.message);
}
});
我想做什么:
$.ajax({
url: /api/form/post,
contentType: "application/json; charset=UTF-8",
type: "POST",
dataType: "json",
data: JSON.stringify(formData),
success: function (resp) {
console.log("Updated successfully!"); },
error: function (request, status, error) {
console.log(request.responseText + ' Error:' + error.message);
}
});
如果删除localhost:3227/api/form/post
并且只保留localhost:3227
,AJAX调用如何知道/api/form/post
的位置
是否在某些项目配置中我不知道?
答案 0 :(得分:1)
在配置文件中添加, var api_url = localhost:3227;
然后,
$.ajax({
url: api_url+ '/api/form/post',
contentType: "application/json; charset=UTF-8",
type: "POST",
dataType: "json",
data: JSON.stringify(formData),
success: function (resp) {
console.log("Updated successfully!"); },
error: function (request, status, error) {
console.log(request.responseText + ' Error:' + error.message);
}
});