测试/构建MSDN文章“带填充的JSON(AJAX)" [英] Testing/Building MSDN article "JSON with Padding (AJAX) "

查看:62
本文介绍了测试/构建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"拒绝访问"和"method =?"抛出此错误:URI:

if i run http://yourserver.com/CustomerService.svc/getcustomer?method=foo' on the remote domain i get access denied error AND if i run http://yourserver.com/CustomerService.svc/getcustomer?method=?' i get this error(see below) and i have exactly what you have in your sample page. it seems that "method=foo" "get access denied" and "method=?" throw this error: URI:

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

UPDATE3

UPDATE2

我已经在两种不同的情况下进行了测试:

i have tested with two different scenario:

1)可以与本地主机正常工作

 $.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

Message: Expected ';' Line: 1 Char: 11 Code: 0

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

UPDATE2

UPDATE1

当我在浏览器中运行服务时 http://yourserver.com/CustomerService.svc/getcustomer 我得到一个弹出窗口,要求我进行细化或保存文件,因此我将文件保存到浏览器中,然后在记事本中打开它,然后看到数据,因此将其返回给我jsonp.

when i run the service in the browser http://yourserver.com/CustomerService.svc/getcustomer i get the the popup window asking for me to fine or save the file, so i save the file to browser and i open it in the notepad and i see that the data so its returning me the jsonp.

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

您到底要我在按钮的哪个位置放置return false?

where exactly you want me to put return false in the button?

在调试器上,我只是为了插入代码而已.

on the debugger i put just for steping in the code.

UPDATE1

更新: 以下是我如何致电我的跨域服务.

UPDATE: below is how i am calling to my cross domain service.

 $(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日,星期五,UTC时间

User Agent: Mozilla/4.0 (compatible; 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) Timestamp: Fri, 19 Nov 2010 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(AJAX)

I've been learning and building JSONP Web services using WCF on fx3.5. I finally got a sample togather with the help of this article and MSDN article JSON with Padding (AJAX)

我正在本地运行

http://localhost:4058/WiringJsonImpl.svc

进行测试.

它没有显示标准的wsdl屏幕,而是显示消息当前已禁用此服务的元数据发布"

instead of getting the standard wsdl screen it display the message "Metadata publishing for this service is currently disabled"

我将地址粘贴到网址中

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

然后按Enter键,我将在IE浏览器中看到带有此消息的黑屏.

and hit enter i see the blank screen with this message in IE browser.

The webpage cannot be found 
 HTTP 400  

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

in my web.conig i see the error but does not stop running the application:

 <bindings>
   <customBinding>
    <binding name="jsonpBinding">
     <jsonpMessageEncoding/> //<<<error here
     <httpTransport manualAddressing="true"/>
    </binding>
   </customBinding>
  </bindings>

推荐答案

在提供的示例应用程序(JSONP)中,服务方法称为GetCustomer,而不是GetLocation.这么称呼:

In the provided sample application (JSONP) the service method is called GetCustomer and not GetLocation. So to call it:

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

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

which returned the following response in my browser:

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

Visual Studio Intellisense强调<jsonpMessageEncoding/>的原因是因为这是一个自定义绑定元素,而不是架构的一部分,并且未知.您可以放心地忽略此警告.

The reason that Visual Studio Intellisense underlines the <jsonpMessageEncoding/> is because this is a custom binding element and not part of the schema and it is not known. You can safely ignore this warning.

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

The only problem with this sample is that the files are packaged in a Class Library instead of Web Application. In order to run the service more easily in the Visual Studio built in server you could create a new Web Application, copy all the files to this new application and run it. You just need to modify this line in web.config:

<add name="jsonpMessageEncoding"
     type="Microsoft.Ajax.Samples.JsonpBindingExtension, service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
                                                            ^

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

to include your web application name:

<add name="jsonpMessageEncoding"
     type="Microsoft.Ajax.Samples.JsonpBindingExtension, MyWebAppName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
                                                              ^

仅此而已.现在,继续编写使用此JSONP流的客户端应用程序. jQuery在 $.getJSON() 方法中做得很好:

That's all. Now go ahead and write a client application which consumes this JSONP stream. jquery does a great job with the $.getJSON() method:

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

我已经在这里上传了工作版本.

这篇关于测试/构建MSDN文章“带填充的JSON(AJAX)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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