2017-04-14 5 views
2

私は、ASP.NET MVCアプリケーションからのjquery ajax投稿を使用して、OWIN経由でWindowsサービスでホストされているWeb APIを呼び出しています'オプション)。統合されたWindows認証を追加することになるまで、それは働いていました。私はxhrFields:{withCredentials:true}を$ .ajax呼び出しに追加して、クライアントの認証側を完成させました。今度は返されるデータはnullです。ここで

は私のサーバー側のスタートアップである:ここで

public class Startup 
{ 
    public void Configuration(IAppBuilder appBuilder) 
    { 
     var config = new HttpConfiguration(); 

     var listener = 
      (HttpListener)appBuilder.Properties["System.Net.HttpListener"]; 
     listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication; 

     //Maps Http routes based on attributes 
     config.MapHttpAttributeRoutes(); 
     config.Filters.Add(new AuthorizeAttribute()); 

     //Enable WebApi 
     var cors = new EnableCorsAttribute("*", "*", "*"); 
     cors.SupportsCredentials = true; 
     config.EnableCors(cors); 

     appBuilder.UseWebApi(config); 
    } 
} 

は、Web APIのメソッドです:

公共HttpResponseMessage PostSomething([FromBody]文字列ストリームdataIn)FYI

、文字列があまりにもすることができURIに渡すべき大きい。

function TestAjax() { 
     $.ajax({ 
      url: 'http://localhost:8080/api/Test/PostSomething', 
      xhrFields: { withCredentials: true }, 
      type: 'post', 
      data: 'test' 
     }).done(function (response) { 
      alert(response); 
     }); 
    } 

ストリームdataInは常にnullである:ここでは

は$アヤックスの呼び出しです。

答えて

0

私は答えを見つけましたが、これは簡単ですが、なぜこれがなぜ機能するのかについての詳細はまだ分かりません。 "data: 'test'"を "data:{'': 'test'}"に変更するだけです。

function TestAjax() { 
    $.ajax({ 
     url: 'http://localhost:8080/api/Test/PostSomething', 
     xhrFields: { withCredentials: true }, 
     type: 'post', 
     data: { '': 'test' } 
    }).done(function (response) { 
     alert(response); 
    }); 
} 

誰でも知っている人は、返信をしてください。ありがとう!

関連する問題