【问题标题】:Post strings to WCF service endpoint with jQuery.ajax (500)使用 jQuery.ajax (500) 将字符串发布到 WCF 服务端点
【发布时间】:2012-10-31 22:07:25
【问题描述】:

这个问题已经在许多帖子中以类似的方式提出,但我没有一个可行的解决方案。

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

【问题讨论】:

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


    【解决方案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&amp;pwd=pwd&amp;domain=domain",
    然后你不需要在你的ajax调用中设置“数据”......看看这是否有效。

    【讨论】:

    • 这是一个登录功能,所以我需要它是POST
    【解决方案2】:

    尝试使用如下代码:

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

    也适用于您向合同传递额外的两个空参数。

    【讨论】:

    • null 参数是为了处理我们在 IE8 中看到的一些问题。我也试过没有空参数但没有运气。
    【解决方案3】:

    ::sigh:: 已通过将 wsSecureHttp 绑定替换为 wsHttp 来纠正此问题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多