【问题标题】:How to call a cross domain WCF Service from JQuery Ajax with Parameters如何使用参数从 JQuery Ajax 调用跨域 WCF 服务
【发布时间】:2016-10-22 18:12:23
【问题描述】:

我有一个跨域 WCF。我已经在服务上实现了 CORS。

如果方法没有任何参数,那么我可以使用 JQuery Ajax 调用它。

但问题是,当方法有任何参数时,在 JQuery Ajax 调用之后会发生一些错误。

  $.support.cors = true;
  $.ajax({
    url: servicePath,
    crossDomain: true,
    type: 'POST',
    async: false,
    cache: false,
    data: '{"name": "xyz"}',
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    processData: true,
    success: function (response) {
      alert('s');
    },
    error: function (error) {
      alert(error.status + ' : ' + error.statusText);
    }
  })

Server Side Code

【问题讨论】:

  • 你的服务是 webapi 吗?你能在 url 中传递参数吗,就像 yourservce.svc/parameter1/parameter2 一样?
  • 它不是 webapi。它是 wcf 应用程序。方法类型是'post'。
  • 既然你可以在没有参数的情况下到达你的端点,也许这个链接可以帮助你:stackoverflow.com/questions/3876784/…

标签: jquery ajax wcf cross-domain


【解决方案1】:

您能否提供完整的错误消息?你的 wcf web.config 字段是什么。 我猜你可以在你的服务文件中添加这个属性。 如下图:[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 { [WebGet(ResponseFormat = WebMessageFormat.Json)] public Customer GetCustomer() { return new Customer() { Name = "Pranay", Address = "1 Ahmedabad" }; } }

【讨论】:

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