开发者

Testing/Building MSDN article "JSON with Padding (AJAX) "

开发者 https://www.devze.com 2023-01-26 07:53 出处:网络
UPDATE 3 var local = \'http://localhost:1163/CustomerService.svc/getcustomer?method=?\'; var dev = \'http://yourserver.com/CustomerService.svc/GetCustomer?method=?\';

UPDATE 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);
            });

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) works fine with localhost

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

2) does not work with cross domain

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

                alert(customer.Address);
                alert(customer.Name); 
            });

i get this error:

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

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

UPDATE2

UPDATE1

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"}

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开发者_如何学JAVA 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); 
            });
        });
    });

Webpage error details

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

i get the above error:

Update end

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)

i am running the service on my local

http://localhost:4058/WiringJsonImpl.svc

to test it out.

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

i paste the address in the url

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

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

The webpage cannot be found 
 HTTP 400  

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>


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"} );

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.

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"/>
                                                            ^

to include your web application name:

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

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); 
});

I have uploaded my working version here.

0

精彩评论

暂无评论...
验证码 换一张
取 消