跨域jQuery Ajax请求和WCF REST服务 [英] Cross Domain jQuery Ajax Request & WCF REST Service

查看:112
本文介绍了跨域jQuery Ajax请求和WCF REST服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am calling (Ajax Request) a WCF REST service and the request is a cross domain request.

如果我将服务部署在相同的域中,那么一切都将像奶油一样.最终在生产中,该服务将位于不同的域中.

If I deploy my service in the same domain, everything works like cream. Eventually in production, the service will be in a different domain.

我正在使用jQuery 1.5.2.我的服务返回了一条错误消息:

I am using jQuery 1.5.2. My service returns me an error saying:

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

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

Although in Firefox I can see the JSON values but execution falls to the error handler of Ajax request.

我的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:

On WCF Service side, I have configured CrossDomainScriptAccess to true:

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

我从服务器获得的

JSON响应是:

JSON response which I get from the server is:

[{"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)\/"}]

我在设置中缺少任何内容吗?如果将服务移至同一域,则整个代码都可以正常工作.

Am I missing anything here in the settings. Entire code works fine if the service is moved to the same domain.

我看过类似的帖子,但无法完成这项工作.

I looked into similar post but couldn't make this work.

推荐答案

好吧,我自己弄清楚了.解决方案是修改包含服务详细信息的配置文件

Well I figured out on my own. Solution was to modify the configuration file holding the service details

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

I added Standard Endpoint and the binding in the config file

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



  <bindings>

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

这篇关于跨域jQuery Ajax请求和WCF REST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆