使用jQuery.ajax(500)将字符串发布到WCF服务端点

时间:2012-10-31 22:07:25

标签: asp.net ajax json wcf web-services

在没有可行的解决方案的情况下,已经以类似的方式在一些帖子中提出了这个问题。

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() {
        // ...
    }
});

3 个答案:

答案 0 :(得分: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: "/Ajax/Cart/ManagerDiscountService.svc/ToggleMode?userId=uid&pwd=pwd&domain=domain",
然后你不需要在你的ajax调用中设置'data'...看看是否有效。

答案 1 :(得分: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() {
        // ...
    }
});

也是为了向合同传递额外的两个空参数。

答案 2 :(得分:0)

:: sigh ::通过将wsSecureHttp绑定替换为wsHttp

来解决此问题
<endpoint address="" behaviorConfiguration="Item.ItemAspNetAjaxBehavior"
 binding="webHttpBinding" bindingConfiguration="wsHttp" 
 contract="Cart.ManagerDiscountService" />