1

サーバーにクロスドメイン jquery ajax リクエストを送信しています。

$.ajax({
beforeSend: function (xhr) {
    xhr.withCredentials = true;
},
data: data,
type: "GET",
url: requestUrl,
xhrFields: {
    withCredentials: true
},
async: true,                      
dataType: 'json',
crossDomain: true
}; 

送信されるデータ オブジェクトの形式は次のとおりです。

var data = {
    Customer: { id: 1 },
    Order: { id: 1 }
};

データは JSON.stringify(data) で変換され、サーバーに送信されます。

サーバーには、次のリクエスト オブジェクトがあります。

public class RequestObject
{
   public CustomerRef Customer { get; set; }
   public OrderRef Order { get; set; }
}

両方のオブジェクトにはまだ id プロパティがあります。

サーバー側でデバッグすると、リクエスト オブジェクトが作成されますが、プロパティ Customer と Order の両方が null です。

データ オブジェクトにパラメータを指定してデータをリクエスト (GET) しています。

私の送信したURLは次のようになります。

http://localhost:82/json/reply/MyService?{%22Customer%22:{%22id%22:1},%22Order%22:{%22id%22:1}}

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

4

1 に答える 1

1

同様の問題がありました。解決策は、.ajax 呼び出しで contentType プロパティを「application/json」に設定することでした。これを行った後、すべてのシリアル化がうまくいきました。

ここに私のテストコードがあります:

var dataObject = {RequestString: "hello service!", RequestDetails: {ClientName: "val1", ClientGroupID: "val2"}, Arguments: {dict1: {test1: "test1", test2: "test2"}}};

var dataString = JSON.stringify(dataObject);

var request = $.ajax({
url: "http://localhost:1337/testws",
type: "POST",
dataType: "json",
contentType: "application/json",        
data: dataString,
cache: false,
success: function (d) {
    $("#result-div").text(JSON.stringify(d));
},
error: function (jqXHR, textStatus) {
    alert("web call failed! d" + textStatus);
}
});
于 2013-07-25T11:33:21.180 に答える