2012-10-31 29 views
0

此問題已經以類似方式在許多帖子中提出,但沒有可行的解決方案。使用jQuery.ajax將字符串傳遞給WCF服務端點(500)

ManagerDiscountService我的服務端點的樣子:

[ServiceBehavior] 
[ServiceContract(Namespace = "Cart")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class ManagerDiscountService : CartService 

    [OperationContract] 
    // also tried [WebInvoke(RequestFormat=WebMessageFormat.Json)] 
    // also tried [WebInvoke(RequestFormat=WebMessageFormat.Json, Method="POST")] 
    public MyObject ToggleMode(string userId, string pwd, string domain) 

的web.config:

<service name="Cart.ManagerDiscountService"> 
    <endpoint address="" behaviorConfiguration="Item.ItemAspNetAjaxBehavior" 
    binding="webHttpBinding" bindingConfiguration="wsSecureHttp" 
    contract="Cart.ManagerDiscountService" /> 
</service> 
<!-- tried adding a similar wsHttp binding since the POST is not SSL, no luck --> 

我嘗試後userIdpwddomain這個終點,但我看到的是500的。 爲什麼不公佈這項工作方式?當我在Chrome調試,error總是在$.ajax接下來執行:

params = { "userId": "user", "pwd": "password", "domain": "mydomain" }; 
$.ajax({ 
    type: "POST", 
    url: "/Ajax/Cart/ManagerDiscountService.svc/ToggleMode", 
    dataType: "json", 
    data: JSON.stringify(params, null, null), 
    contentType: "application/json; charset=utf-8", 
    success: function (data) { 
     // ... 
    }, 
    error: function() { 
     // ... 
    } 
}); 

回答

0

::嘆息::這是通過更換wsSecureHttpwsHttp

<endpoint address="" behaviorConfiguration="Item.ItemAspNetAjaxBehavior" 
binding="webHttpBinding" bindingConfiguration="wsHttp" 
contract="Cart.ManagerDiscountService" /> 
0

嘗試使用的代碼如下:

params = { "userId": "user", "pwd": "password", "domain": "mydomain" }; 
$.ajax({ 
    type: "POST", 
    url: "/Ajax/Cart/ManagerDiscountService.svc/ToggleMode", 
    dataType: "json", 
    data: JSON.stringify(params), 
    contentType: "application/json; charset=utf-8", 
    success: function (data) { 
     // ... 
    }, 
    error: function() { 
     // ... 
    } 
}); 

也爲你所傳遞額外的兩個空參數合同。

+0

結合修正空參數來處理,我們在IE8中發現了一些問題。我也試過沒有null參數,沒有運氣。 –

1

你只需要一個我猜的uri模板。 我已編輯你的代碼在這裏:
[OperationContract]
[WebGet(UriTemplate="ToggleMode?userId={userId}&pwd={pwd}&domain="{domain}", ResponseFormat=WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

在其中方法將看起來像:
MyObject ToggleMode(string userId, string pwd, string domain);

而且,你的AJAX調用的URL會看起來像:
url: "/Ajax/Cart/ManagerDiscountService.svc/ToggleMode?userId=uid&pwd=pwd&domain=domain",
然後你不需要設置你的ajax調用中的'數據'...看看是否有效。

+0

這是一個登錄功能,所以我需要它是'POST' –

相關問題