2

PhoneGap と jQuery Mobile を使用してモバイル アプリケーションを開発しています。私の目的は、クライアント (モバイル) がデータベースに対してクエリを実行できるようにする Web サービスを作成することです。

いくつかの調査の後、AJAX Enabled Services が探していたものである可能性があることがわかりました。そこで、AJAX 対応の WCF サービスを作成することから始め、今のところ、次のメソッドのみを追加しました。

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
public string GetString()
{
    return "Hello there";
}

私の web.config は次のようになります。

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="WebApplication1.MobileServiceAspNetAjaxBehavior">
                    <enableWebScript />
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
        <services>
            <service name="WebApplication1.MobileService">
                <endpoint address="" behaviorConfiguration="WebApplication1.MobileServiceAspNetAjaxBehavior"
                    binding="webHttpBinding" contract="WebApplication1.MobileService" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

このサービスを終了した後、次の方法を使用してクライアントから呼び出しました。

$.ajax({
    type: "POST",
    url: "http://localhost:11634/MobileService.svc/GetString",
    contentType: "application/json",
    data: "{}",
    dataType: "json",
    success: function (result) {
    $("#textbox").text(result);
    },        
    error: function (textStatus) {
        alert(textStatus);
    }
});

サービスを呼び出すと、次のエラーが発生します[object Object]。私が間違っていることと、正しいテクノロジーを使用しているかどうかについて教えてもらえますか?

4

3 に答える 3

1

Tariqulazam が正しく指摘しているように、[Object object] はエラーではなく、応答オブジェクトです。データにアクセスするには、コードを次のように変更します。

success: function (result) {     
    var data = result.d
    $("#textbox").text(data);     
},

教科書の例を見たい場合、次は WCF Web サービスを使用する jQuery コードの良い例のように見えます。

jQuery を使用した WCF サービスの使用

お役に立てれば!

于 2012-10-17T08:36:55.003 に答える
0

$ .ajax({...})を関数呼び出し内にラップする必要があります。そのように特定のアクションでそれが呼び出されます。

于 2012-10-16T17:30:03.593 に答える
0

パラメータを渡していないので、代わりにこれを GET 呼び出しに変更し、ajax 呼び出しのデータ部分を削除します。また、それを返す方法は XML 形式でした。応答形式を JSON に変更します。

    [OperationContract]
    [WebGet(UriTemplate = "/GetString", RequestFormat = WebMessageFormat.Json)]
    public string GetString()
    {
        return "Hello there";
    }

$.ajax({     
    type: "GET",     
    url: "http://localhost:11634/MobileService.svc/GetString",     
    contentType: "application/json",     
    dataType: "json",     
    success: function (result) {     
    $("#textbox").text(result);     
    },             
    error: function (textStatus) {     
        alert(textStatus);     
    }     
});
于 2012-10-15T23:04:19.687 に答える