私はこれを検索しましたが、私に役立つものを見つけることができなかったので、これが投稿されていて、それを見つけることができなかった場合はお詫び申し上げます。
IISでホストされているWCFサービスアプリケーションを作成しました。現在、基本的に国名とそのコードをjsonオブジェクトとして返すためのhelloworldメソッドを使用した非常に基本的な方法です。
また、リストオブジェクトにデータを入力する目的で、メソッドをリモートで呼び出すjqueryもいくつか作成しました。
現在、メソッドを呼び出すと、ajax呼び出しの成功パラメーターにヒットし、「未定義」で警告されます。これの原因はわかりませんが、ばかげた間違いを犯した可能性があります。
これがサービスとjqueryのコードです
Web構成:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="None" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true"/>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
service1.svc
<%@ ServiceHost Language="C#" Debug="true" Service="RestfulFlightWCF.Service1" codeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
service1.svc.cs {//注:[リファクタリング]メニューの[名前の変更]コマンドを使用して、コード、svc、および構成ファイルのクラス名「Service1」を一緒に変更できます。
[ServiceContract(Namespace = "JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public Country GetCountry(string id)
{
Country county = new Country();
county.Name = "United Kingdom";
county.Id = "gb";
return county;
}
[DataContract]
public class Country
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
}
jquery
$(document).ready(
function () {
$.ajax({
type:"GET",
Data:'gb',
Url:"http://192.168.1.6:80/FlightServices.svc/GetCountry",
DataType:"jsonp",
method:"GetCountry",
success: function(msg){
debugger;
alert(msg.responseText);
if (msg.responseText) {
var err = msg.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
},
failure: function(){
alert("something went wrong");
},
error: function(){
alert("something happned");
}
});
});
長い投稿で申し訳ありませんが、コードを含めると役立つと思いました。