私は2つのサイトを持っています。
私もウェブサービスを持っています。
http://www.mydomain.com/trouwlocaties/zoeken-uitgebreidのカスケード ドロップダウンで国名を読み込むと、これが実際に動作しているのがわかります。
ただし、同じ Web サービスが次のエラーをスローします : http://otherdomain.com/weddingvenues/search-advanced、クライアントが .asmx サービスを取得しようとする場所で、toptrouwen で POST を使用します (これは、起こるはずのことであり、より安全であると私は信じています)。
これは GetCountries Web サービスです。
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<ToolboxItem(False)> _
Public Class geolocation
'<System.Web.Script.Services.ScriptService()> _
'<WebService(Namespace:="http://tempuri.org/")> _
'<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
'<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetCountries(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
Dim values As New List(Of CascadingDropDownNameValue)
Dim myConnection As SqlConnection = GetConnection()
Dim cmd As New SqlCommand(String.Format("SELECT id,name as title FROM country order by title asc", Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName), myConnection)
Try
myConnection.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader
Dim CountryName As String
Dim CountryID As Integer
While reader.Read
CountryName = reader("title").ToString
Int32.TryParse(reader("id"), CountryID)
values.Add(New CascadingDropDownNameValue(CountryName, CountryID.ToString))
End While
Catch ex As Exception
Finally
myConnection.Close()
End Try
Return values.ToArray
End Function
End Class
まず、これをweb.configに追加してみました:
<system.web>
<webServices>
<protocols>
<remove name="Documentation"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
それを行った後、Chrome コンソールでこれを受け取りました。
Uncaught SyntaxError: Unexpected token <
どうやら結果はXMLとして解釈されなかったようですが、私の推測ではJSONです。Google で検索した結果、これは MIME タイプに関係していると思いましたが、このサービスの XML に変更する方法がわかりませんでした。
だから私は検索を続け、何か他のものを見つけました。私はこれらの投稿を読んでいまし た :トークン < -- jQuery ajax で
どうやら「クロスドメインの問題」である可能性があります。
だから私はこれらのファイルを作成することになりました:
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
crossdomain.xml
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<?xml version="1.0" ?>
<cross-domain-policy>
<allow-access-from domain="*" />
<allow-access-from domain="*.otherdomain.com" secure="false" />
</cross-domain-policy>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="GetCountries" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.mydomain.com/geolocation.asmx"
binding="basicHttpBinding" name="GeoLocation" />
</client>
</system.serviceModel>
</configuration>
最初のリンクの例では、ユーザーが属性 bindingConfiguration="DashboardServiceSoap" および contract="DashboardService.DashboardServiceSoap" も追加しましたが、私のケースではそこに何を入力する必要があるかわかりません。
私はまだ立ち往生しています。何が正しいトラックで、どのようにセットアップを構成すればよいかわかりません。
2013年6月21日更新
私のweb.configを次のように更新しました:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
次の4つの構成も試しました。
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
<ToolboxItem(False)> _
Public Class geolocation
Inherits System.Web.Services.WebService
シナリオ 1 および 2 このメソッド定義では:
<WebMethod()> _
Public Function GetCountries(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
シナリオ 1: web.config の WITH プロトコル セクション
<webServices>
<protocols>
<remove name="Documentation"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
.nl ドメインで正しく動作する .com ドメインでメソッド エラー -1 をスローします。Chrome コンソールの表示: Uncaught SyntaxError: Unexpected token < GetCountries:1
シナリオ 2: web.config のプロトコル セクションなし
.nl ドメインで正しく動作する .com ドメインでメソッド エラー -1 をスローします。Chrome コンソールの表示: GET http://www.otherdomain.com/geolocation.asmx/GetCountries?knownCategoryValues=%22%22&category=%22Country%22&callback=Sys._jsonp0 500 (内部サーバー エラー) ScriptResource.axd:7773
このメソッド定義を使用したシナリオ 3 および 4:
<WebMethod()> _
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=System.ServiceModel.Web.WebMessageFormat.Json)> _
Public Function GetCountries(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
シナリオ 3: web.config の WITH プロトコル セクション
<webServices>
<protocols>
<remove name="Documentation"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
.nl ドメインでメソッド エラー 500 をスローします。Chrome コンソールの表示: POST http://www.mydomain.com/geolocation.asmx/GetCountries 500 (内部サーバー エラー) catcher.js:197 ドロップダウンの .com ドメインでメソッド エラー -1 をスローします。Chrome コンソールの表示: Uncaught SyntaxError: Unexpected token < GetCountries:1
シナリオ 4: web.config のプロトコル セクションなし
.nl ドメインでメソッド エラー 500 をスローします。Chrome コンソールの表示: リソースの読み込みに失敗しました: サーバーは 500 のステータスで応答しました (内部サーバー エラー) ドロップダウンの .com ドメインでメソッド エラー -1 をスローします。Chrome コンソールの表示: GET http://www.otherdomain.com/geolocation.asmx/GetCountries?knownCategoryValues=%22%22&category=%22Country%22&callback=Sys._jsonp0 500 (内部サーバー エラー)
また、スクリプトから .asmx を明示的に呼び出しているわけではありません。カスケード ドロップダウンにその作業を任せています。そのようです:
<asp:DropDownList ID="ddlCountries" CssClass="textbox" AutoPostBack="true" runat="server"></asp:DropDownList>
<cc1:cascadingdropdown ID="cddCountries" runat="server" Category="Country" Enabled="True" LoadingText="<%$Resources:Glossary,loading %>" PromptText="<%$Resources:Glossary,country_choose %>"
ServiceMethod="GetCountries" TargetControlID="ddlCountries">
</cc1:cascadingdropdown>
コードビハインド
cddCountries.ServicePath = "http://www.mydomain.com/geolocation.asmx"
これらの事前定義された要素を使用しているという事実が私の問題と関係があるかどうかはわかりません。自分でスクリプトを介して .asmx サービスを呼び出し、ドロップダウンに入力することをお勧めします。もしそうなら:私はそうする方法がわかりません。