2016-07-18 7 views
1

私はソリューションの下で2つのプロジェクトで1ページずつ持っています。私はMVCを使用していません。c#asp.netのWebページにアクセス制御ヘッダーを設定する方法は?

以下は、home.aspxACCEPTORプロジェクトに追加したコードです。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="home.aspx.cs" Inherits="ACCEPTCORS.home" %> 
<% Response.AppendHeader("Access-Control-Allow-Origin", "*"); %> 
<% Response.AppendHeader("Access-Control-Allow-Methods","*"); %> 

私が呼び出すWebMethodは以下のとおりです。私は以下のCORSとして命名他のプロジェクトの他のページがあるhome.aspx.csファイル

[WebMethod] 
    public static int show() 
    { 
     int result = 0; 

     return result; 
    } 

でコードを書かれています。このページから私はJavaScript関数を呼び出しています。 javascript関数は、home.aspx.csファイルのWebMethodを呼び出しています。

<head runat="server"> 
<title></title> 
<script src="http://localhost:5314/home.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 

<form id="form1" runat="server"> 
<div> 
<input type="button" onclick="show();" value="fetch data"/> 
</div> 
</form> 

アプリケーションをホストし、私はアプリケーションを実行するとJS http://localhost/[applicationName]/home.aspx/show

答えて

0

の下 javascriptのコードがあり、 JSファイルにアクセスできます
+0

にアクセスしようとすることができます

function show() { $.ajax({ type: 'POST', url: 'http://localhost:5314/home.aspx/show', dataType: "json", async: true, success: function (data) { alert(data.d); }, failure: function (data) { alert('failure'); }, error: function (data) { alert('error'); }, }) } 

+0

[WebMeth OD]アプリケーション –

+0

は、アプリケーションの設定ファイルにこの設定を追加 <名前= "アクセス制御 - 許可 - 起源" 値を追加= "*" />

関連する問題