1

バックグラウンド:

私は非常にうまく機能しているWCFサービス(.NET 3.5)を作成しました。JavaScriptでサービスを使用する必要があり、ドメイン間の制限により(サービスはJavaScriptを含むWebページとは異なるサーバーにあります) 、標準のソープポストが機能していませんでした。いくつかの構成を変更し、Microsoft ブログの投稿に従ってメソッドに WebInvoke 属性を追加し、GET で動作するサービスを取得し、soapUI でテストしてメソッドが動作していることを確認しました。私は JSFiddle で例をセットアップしようとしていましたが、これはイントラネット サービスであるため、明らかに動作させることができませんでした。

これが私の SVC.cs ファイルのメソッドです: (これを理解しようとして、いくつかのコード変更と構成ファイルへの変更を行いました)

    // On the interface that defines the contract
    [OperationContract]
    [WebGet]
    string Post(string questionid, string answervalue);

    // In my actual service file code behine.
    public string Post(string questionid, string answervalue)
    {

        Guid dataid = _dataProvider.Post(questionid, answervalue);
        _dataProvider.Log(retval.ToString());
        return dataid.ToString();
    }

URL を入力するだけで、その値を表す GUID の文字列表現が返されます。

   http://localhost/WebPostSvc.svc/json/Post?questionid=207&answervalue=207009
   returns: "<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">04bb6445-b1af-4214-8f8b-cf66bb15467f</string>"

ここに私が仕事をしようとしているjavascriptがあります:

<script type="text/javascript">

    // string functions to allow formatted output
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/{(\d+)}/g, function(match, number) { 
        return typeof args[number] != 'undefined'
          ? args[number]
          : match
        ;
      });
    };

    $(function() 
    {

        $('.testpostbtn').click(function() {
            $.jsonp({
                url: "ttp://localhost/WebPostSvc.svc/json/Post?questionid=207&answervalue=207009",
                callbackParameter: "callback",
                "success" : function(data) {
                   alert(data);

                },
                "error" : function(d, msg) {
                        alert(
                            "Could not post the data\ncallback={0}\ncallbackParameter={1}\nurl={2}\n{3}\n"
                               .format(d.callback, d.callbackParameter, d.url, msg)
                        );
                    }
            });
            alert('request sent!');

        });
    });


</script>

$.jsonp メソッドは次のとおりです。

// jquery.jsonp 2.3.1 (c)2012 Julian Aubourg | MIT License
// https://github.com/jaubourg/jquery-jsonp

対処しなければならないのは、アラート ボックスに送信したメソッドから返されたエラーだけです。

Could not post the data
callback=_jqjsp
callbackParameter=callback
url=http://localhost/WebPostSvc.svc/json/Post?questionid=207&answervalue=207009
error

返される唯一のエラー メッセージは "error" で、あまり役に立ちません。

メソッドを間違って使用しているだけだと確信しており、次のことを知る必要があります。

私は何を間違っていますか?

4

1 に答える 1

0

1)クロスドメインをWCFで機能させるには、おそらくバインディングを構成する必要があります。

http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/からコピーしたサンプル。crossDomainAccessEnabled属性を参照してください。

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webHttpBehavior">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <bindings>
    <webHttpBinding>
      <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
    </webHttpBinding>
  </bindings>
  <services>
    <service name="ServiceSite.CustomersService">
      <endpoint address="" binding="webHttpBinding"
                bindingConfiguration="webHttpBindingWithJsonP"
                contract="ServiceSite.CustomersService"
                behaviorConfiguration="webHttpBehavior"/>
    </service>
  </services>
</system.serviceModel>

2)利用可能なデバッグツールを使用します。Chromeには優れた開発者ツールが組み込まれており、Firefoxではfirebugをインストールできます。これらのツールは両方とも、ネットワークリクエストをキャプチャし、いくつかの情報を提供できます。

一般に、リクエストが発生せず、それでもエラーが発生する場合は、関数呼び出しに問題があります。リクエストが失敗した場合は、サーバー上の何かがブロックされています。通常、あなたは応答を見て、より多くの情報を得ることができます。応答が正常であるのに失敗した場合は、データ形式に問題があります。

于 2012-07-02T19:13:14.940 に答える