测试/构建MSDN文章“带填充的JSON(AJAX)”

更新3

var local = 'http://localhost:1163/CustomerService.svc/getcustomer?method=?'; var dev = 'http://yourserver.com/CustomerService.svc/GetCustomer?method=?'; $.getJSON(dev, function (customer) { alert(customer.Address); alert(customer.Name); }); 

如果我在远程域上运行http://yourserver.com/CustomerService.svc/getcustomer?method=foo'我会获得访问被拒绝错误并且如果我运行http://yourserver.com/CustomerService.svc/getcustomer?method=?' 我收到此错误(见下文),我在您的示例页面中拥有您所拥有的内容。 似乎“method = foo”“get access denied”和“method =?” 抛出此错误:URI:

 "http://yourserver.com/CustomerService.svc.svc/GetCustomer?method=jsonp1290197214042" 

UPDATE3

UPDATE2

我测试了两种不同的场景:

1) 与localhost一起正常工作

  $.getJSON('http://localhost:1163/service.svc/getcustomer?method=?', function (customer) { alert(customer.Address); alert(customer.Name); }); 

2) 不适用于跨域

  $.getJSON('http://yourserver.com/CustomerService.svc/getcustomer?method=?', function (customer) { alert(customer.Address); alert(customer.Name); }); 

我收到此错误:

消息:预期’;’ 行:1个字符:11个代码:0

 URI: http://yourserver.com/CustomerService.svc/GetCustomer?method=jsonp1290183992345 

UPDATE2

UPDATE1

当我在浏览器http://yourserver.com/CustomerService.svc/getcustomer中运行服务时,我得到弹出窗口要求我保存或保存文件,所以我将文件保存到浏览器,然后在记事本,我看到数据,所以它返回我的jsonp。

 {"Address":"1 Example Way","Name":"Bob"} 

你想让我在按钮中输入false的确切位置?

在调试器上,我只是为了在代码中进行步骤。

UPDATE1

更新:以下是我如何调用我的跨域服务。

  $(document).ready(function () { $("#driver").click(function (event) { $.getJSON('http://yourserver.com/CustomerService.svc/getcustomer?method=?', function (customer) { debugger alert(customer.Address); alert(customer.Name); }); }); }); 

网页错误详情

用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 5.1; Trident / 4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath。 2; MS-RTC LM 8; .NET4.0C; .NET4.0E)时间戳:星期五,2010年11月19日14:56:22 UTC

 Message: Expected ';' Line: 1 Char: 11 Code: 0 URI: 'http://yourserver.com/CustomerService.svc/GetCustomer?method=JsonPCallBack 

我得到上面的错误:

更新结束

我一直在fx3.5上使用WCF学习和构建JSONP Web服务。 在这篇文章的帮助下,我终于得到了一个示例,以及MSDN文章JSON with Padding(AJAX)

我正在本地运行该服务

 http://localhost:4058/WiringJsonImpl.svc 

测试一下。

而不是获得标准的wsdl屏幕,它显示消息“此服务的元数据发布当前已禁用”

我将地址粘贴到url中

  http://yourserver.com/LocationService.svc/GetLocation?method=jsonpCallback 

然后按Enter键我在IE浏览器中看到带有此消息的空白屏幕。

 The webpage cannot be found HTTP 400 

在我的web.conig中,我看到错误,但没有停止运行应用程序:

      //<<<error here     

在提供的示例应用程序(JSONP)中,服务方法称为GetCustomer而不是GetLocation 。 所以称之为:

 http://localhost:1163/service.svc/getcustomer?method=jsoncallback 

它在我的浏览器中返回以下响应:

 jsoncallback( {"Address":"1 Example Way","Name":"Bob"} ); 

Visual Studio Intellisense强调的原因是因为这是一个自定义绑定元素,而不是模式的一部分,因此不知道。 您可以放心地忽略此警告。

此示例的唯一问题是文件打包在类库而不是Web应用程序中。 为了在Visual Studio内置服务器中更轻松地运行服务,您可以创建一个新的Web应用程序,将所有文件复制到此新应用程序并运行它。 您只需要在web.config中修改此行:

  ^ 

包括您的Web应用程序名称:

  ^ 

就这样。 现在继续编写一个使用此JSONP流的客户端应用程序。 jquery使用$.getJSON()方法$.getJSON()

 $.getJSON('http://localhost:1163/service.svc/getcustomer?method=?', function(customer) { alert(customer.Address); }); 

我在这里上传了我的工作版本 。