Web API中的跨域请求

编程入门 行业动态 更新时间:2024-10-17 05:37:28
本文介绍了Web API中的跨域请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我遇到问题是我的jquery代码:

Hi All, I am stuck in a problem here is my jquery code:

$(document).ready(function () { $('#btnSubmit').click(function () { var companyId = $("#companyname").val(); var startDate = $("#startdate").val(); var endDate = $("#endate").val(); var codeType = $("#typecode option:selected").text(); var record = "companyid=" + companyId + "&startDate=" + startDate + "&enddate=" + endDate + "&type=" + codeType; var tempurl = 'localhost:49987/api/prospectbounce?' + record; $.ajax({ url: 'localhost:49987/api/prospectbounce?' + record, type: 'GET', async: false, dataType: 'json', success: function (data) { debugger var html = '<table border="1" width="100%">'; html += ' <th align="left"><b>Email Address</b></th>' html += ' <th align="left"><b>Synchronization Key</b></th>' html += '</tr>' $.each(data, function (index, value) { html += '<tr>'; html += '<td align="left">' + value.Email + '</td>'; html += '<td align="left">' + value.SynchronizationKey + '</td>'; html += '</tr>'; }); html += '</table>'; $('#recordShow').html(html); }, error: function (xhr, status, error) { alert(error); }, beforeSend: setHeader }); }); }); function setHeader(xhr) { xhr.setRequestHeader('X-ProspectAppApiKey', '8EJBD8BI:f9a2ac88-6a76-48cf-8357-3f54bb830703'); }

及以下是我的网络API代码

and below is my web API code

protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { // Write your Authentication code here IEnumerable<string> monsterApiKeyHeaderValues = null; if (request.Headers.TryGetValues("X-ProspectAppApiKey", out monsterApiKeyHeaderValues)) { string[] apiKeyHeaderValue = monsterApiKeyHeaderValues.First().Split(':'); if (apiKeyHeaderValue.Length == 2) { var appID = apiKeyHeaderValue[0]; var AppKey = apiKeyHeaderValue[1]; String StrValidationToken = ProspectAccountInfo.IsValidToken(appID, "CRMValidationToken"); Boolean IsValidToken = false; if (!string.IsNullOrEmpty(StrValidationToken)) { //check token if (StrValidationToken.ToString().ToLower() == AppKey.ToString().ToLower()) { IsValidToken = true; } } if (IsValidToken) { var userNameClaim = new Claim(ClaimTypes.Name, appID); var identity = new ClaimsIdentity(new[] { userNameClaim }, "ProspectAppApiKey"); var principal = new ClaimsPrincipal(identity); Thread.CurrentPrincipal = principal; if (System.Web.HttpContext.Current != null) { System.Web.HttpContext.Current.User = principal; } } else { return requestCancel(request, cancellationToken, InvalidToken); } } else { return requestCancel(request, cancellationToken, MissingToken); } } else { // Web request cancel reason APP key missing all parameters return requestCancel(request, cancellationToken, MissingToken); } return base.SendAsync(request, cancellationToken); }

我的问题是当我从跨域发送请求时,我无法处理请求但是使用IE它工作正常。 请帮助我。 感谢&问候 Mohd Wasif

My problem is that when I send request from cross domain I am unable to process request but with IE it work fine. Please help me. Thank & Regards Mohd Wasif

推荐答案

( document )。ready( function (){ (document).ready(function () {

(' #btnSubmit')。点击( function (){ var companyId = ('#btnSubmit').click(function () { var companyId =

( #companyname)。val(); var startDate = ("#companyname").val(); var startDate =

更多推荐

Web API中的跨域请求

本文发布于:2023-11-02 17:03:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Web   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!