Web API中的跨域请求 [英] Cross domain request in Web API

查看:72
本文介绍了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 = 'http://localhost:49987/api/prospectbounce?' + record;

        $.ajax({
            url: 'http://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中的跨域请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆