使用从 .asmx 文件创建的生成的 javascript 时,是否可以添加或自定义标题?
例如,我有一个网络服务,如: SomeService.asmx
[WebService(Namespace = "https://www.somedomain.com/api/someapi", Description = "api", Name = "someapi")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
[HttpPost]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public List<SomeModel> GetData(string SomeArgument)
{
string customHeaderValue = HttpContext.Current.Request.Headers.Get("SomeCustomHeader");
// Do things
}
}
现在使用 jQuery 我可以指定自定义标题,如:
jQuery.ajax({
type: 'POST',
url: '/Path/ToFile/Webservice.asmx/GetData',
data: "data",
success: OnSuccessFunc,
error: OnFailureFunc,
headers: {
'SomeCustomHeader': "SomeCustomHeaderValue" <---
}
});
但我知道 asmx 文件可以生成一些辅助函数 javascript 来为我处理所有事情。例如,如果我添加
<script type="text/javascript" src="/Path/ToFile/Webservice.asmx/js"></script>
然后在页面上我可以简单地写
<script type="text/javascript">
Webservice.GetData("data", OnSuccessFunc, OnFailureFunc)
</script>
但是如何使用生成的 javascript 版本包含自定义标头数据?
答案 0 :(得分:0)
您可以尝试使用 beforeSend 钩子和 $.ajaxSetup() 为每个请求设置标头
看看这个网址。 https://www.edureka.co/community/82342/how-to-add-custom-http-header-to-ajax-request-with-javascript