0

公開されたサーバーで正常にpingを実行して、テストをデモとして返すことができるRESTful Webサービスがあります。次に、呼び出すメソッドにコードを入れようとしましたが、AJAX 呼び出しで失敗したようです。localhostで実行されているサーバーがないため、エラーがどこにあるのかわかりません。構文はデザインのテストと同じであるため、Webサービスが正しく呼び出されていることはわかっています

私はそれを行っていた同様の方法を持っていましたが、RESTful ではなかったので、それに応じてすべてのデータを移行したので、メソッド内のコードが正しく機能することがわかりました。

以下に AJAX 呼び出しを掲載します。

$.ajax({
            type: 'GET',
            url: WEBSERVICE_URL + 'getWebFormDesignFieldContents',
            data: JSON.stringify({
                'pr_id': LOGGED_IN_PR_ID,
                'fe_name': opts,
                '_count': 200,  //this might need to be adjusted slightly.  I may want to make it more OR less.
                '_also_search_fe_desc': true,
                'opts': opts
            }),
            contentType: 'application/json; charset=utf-8',
            dataType: 'jsonp',
            success: function (result) {
                //success
                var r = $(result.getWebFormDesignFieldContentsResult)[0];
                var div = $("<div class='modal'>").html(r.d);
                /*
                var d = document.createElement("div");
                d.className = "modal";
                d.appendChild(r[0]);
                */
                $("div.modal").replaceWith(div);
                $("div.modal #queryInput").val(opts);
                $("div.modal").css({
                    top: $(window).height() / 2 - $("div.modal").height() / 2,
                    left: $(window).width() / 2 - $("div.modal").width() / 2
                });
                $("div.modal").fadeIn();
            },
            error: function (result) {
                //error
                //alert("Error: "+result.statusText);
                alert(result.statusText);

                //$("div.modal").replaceWith($("<div class = 'modal'>").html(result.responseText));
                //$("div.modal").fadeIn();
                $("div.overlay").fadeOut();
            }
        });

Web サービス インターフェイス:

[OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "getWebFormDesignFieldContents")]
    string getWebFormDesignFieldContents(WebFormFieldClass inputData);

Webサービス機能

public string getWebFormDesignFieldContents(string pr_id, string fe_name, string _count, string _also_search_fe_desc, string opts)
    {
       int count = Convert.ToInt32(_count);
       bool also_search_fe_desc = Convert.ToBoolean(_also_search_fe_desc);
       ...
    }

 [DataContract]
public class WebFormFieldClass
{
    [DataMember]
    public string pr_id { get; set; }
    [DataMember]
    public string fe_name { get; set; }
    [DataMember]
    public int count { get; set; }
    [DataMember]
    public string also_search_fe_desc { get; set; }
    [DataMember]
    public string opts { get; set; }
}
4

2 に答える 2

1

編集 :

OKあなたの問題は、JSONオブジェクトをGETでデータとして渡すことでした。

申し訳ありませんが、GET リクエストではクエリ文字列パラメーターを使用します。JSON オブジェクト全体を送信する場合は、POST を使用する必要があります。

また、URIParameter は、このように先頭にスラッシュ「/」を追加します

  /getWebFormDesignFieldContents

url: URL+'/getWebFormDesignFieldContents',

POST リクエストを作成するか、「pr_id」などのパラメーターを UriTemplate パラメーターとして渡す GET を使用する 2 つのオプションがあります。

私はこのように私の最後にテストしました:

 <script src="Jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript">
    var URL = 'http://localhost:11431/Service1.svc/getWebFormDesignFieldContents';
    $.ajax({           
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
            'pr_id': 'ss',
            'fe_name': 'ss',
            '_count': 22,  //this might need to be adjusted slightly.  I may want to make it more OR less.
            '_also_search_fe_desc': true,
            'opts': 'kk'
        }),     
        url: URL,

        success: function (data) {
            alert(data);
        },
        error: function (xhr, status, message) {
            alert("Error: " + status + " " + message);
        }
    });


</script>

私の IService1.svc

 [OperationContract]
    [WebInvoke(Method = "POST",
     ResponseFormat = WebMessageFormat.Json,
     BodyStyle = WebMessageBodyStyle.Wrapped)]

    string getWebFormDesignFieldContents(string pr_id, string fe_name, string _count, string _also_search_fe_desc, string opts);
于 2012-07-06T17:43:24.847 に答える
0

This issue is coming from the browser, or so it seems. All this information was correct, but the errors and issues i was getting are from IE and its hate for ajax. Another issue with this, is that we had to add a bunch of references to the the ASP to allow the program to handle information from the target successfully.

于 2012-07-10T15:18:36.897 に答える