jquery - 跨域 jQuery Ajax 请求和 WCF REST 服务

标签 jquery wcf rest jsonp

我正在调用(Ajax 请求)WCF REST 服务,并且该请求是跨域请求。

如果我在同一个域中部署我的服务,一切都会像奶油一样工作。最终在生产中,该服务将位于不同的域中。

我使用的是 jQuery 1.5.2。我的服务返回一条错误消息:

errorThrown: "jQuery15208493315000087023_1334089616458 was not called"
textStatus: "parsererror"

尽管在 Firefox 中我可以看到 JSON 值,但执行会落到 Ajax 请求的错误处理程序中。

我的 Ajax 请求是:

function CallService() {
    $.ajax({
        type: "GET", 
        url: "http://SomeService/EmpService.svc/GetValues?dv=1455",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp", 
        processdata: false,            
        success: function (data) {
            ServiceSucceeded(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            debugger;
            alert("Service Error");
            ServiceFailed(jqXHR, textStatus, errorThrown);
        }
    });
}

在 WCF 服务端,我已将 CrossDomainScriptAccess 配置为 true:

<webHttpBinding>
  <binding name="webHttpBindingWithJsonP"
           crossDomainScriptAccessEnabled="true" />
</webHttpBinding>

我从服务器得到的 JSON 响应是:

[{"Message": "Stop On Duty", "MessageTime": "\/Date(1334068773893-0500)\/"},
 {"Message": "Start On Duty", "MessageTime": "\/Date(1334068763540-0500)\/"},
 {"Message": "App_testing_4102012924am", "MessageTime": "\/Date(1334068533627-0500)\/"},
 {"Message": "Kunal_testing_4102012924am", "MessageTime": "\/Date(1334067945510-0500)\/"},
 {"Message": "Alert: Door Open", "MessageTime": "\/Date(1334066280963-0500)\/"}]

我是否缺少设置中的任何内容?如果服务移至同一域,整个代码可以正常工作。

我查看了类似的帖子,但无法完成这项工作。

最佳答案

好吧,我自己想出来了。解决方案是修改保存服务详细信息的配置文件

我在配置文件中添加了标准端点和绑定(bind)

<standardEndpoints>
      <webScriptEndpoint>
       <standardEndpoint crossDomainScriptAccessEnabled="true">
       </standardEndpoint>
      </webScriptEndpoint>
      </standardEndpoints>



  <bindings>

  <webHttpBinding>
    <binding name="webHttpBindingWithJsonP"
             crossDomainScriptAccessEnabled="true" />
  </webHttpBinding> 

关于jquery - 跨域 jQuery Ajax 请求和 WCF REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10096449/

相关文章:

javascript - 创建标签时运行js函数

wpf - 如何从 LINQ 转移到 SQL 到 "LINQ to WCF"?

rest - 领英 v2 API : How can upload an image using ugcPosts API?

java - 如何使用 Gradle 项目及其依赖项?

javascript - 使用 React.js 使用身份验证 token 调用 REST API

javascript - jQuery .val() 工作正常 - 除非我将它传递给我真正需要的值

javascript - 如何让侧边栏向上移动

wcf - Extjs上传Excel文件到WCF Restful服务

WCF 错误 109 : here was an error reading from the pipe. 管道在客户端关闭

javascript - 从元素 data-* 属性构建对象