【问题标题】:Adding custom header to generated WebService helper将自定义标头添加到生成的 WebService 帮助程序
【发布时间】:2021-09-25 16:45:13
【问题描述】:

在使用从 .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 版本包含自定义标头数据?

【问题讨论】:

    标签: c# asp.net asp.net-web-api webforms asmx


    【解决方案1】:

    您可以尝试使用带有 $.ajaxSetup() 的 beforeSend 钩子为每个请求设置标头

    看看这个网址。 https://www.edureka.co/community/82342/how-to-add-custom-http-header-to-ajax-request-with-javascript

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2012-04-23
      • 2017-02-15
      • 2016-11-14
      • 1970-01-01
      相关资源
      最近更新 更多