Question

Y at-il un moyen facile d'obtenir JSONP travailler pour les nouveaux services de repos API Web WCF?

Je l'ai essayé avec pas de chance

<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name=""
                      helpEnabled="true"
                      automaticFormatSelectionEnabled="true"
                      defaultOutgoingResponseFormat ="Json"
                      crossDomainScriptAccessEnabled="true"/>
  </webHttpEndpoint>
</standardEndpoints>
Était-ce utile?

La solution

https://alexanderzeitler.com/articles/Look-Ma,-I-can-handle-JSONP-%28aka-Cross-Domain-JSON%29-with-WCF- Web-API et jQuery! /

Mise à jour: Les derniers WCF bits d'API Web livré avec support intégré JSONP alors que l'utilisation est presque similaire à la manière décrite dans le lien ci-dessus.

Autres conseils

You may checkout the following blog post for using JSONP with WCF in .NET 4.0.

Just wanted to provide more detail on WCF WebAPI out-of-the-box support for JSONP. I had a really hard time finding this information, so perhaps it will help somebody else...

This thread over on the WCF CodePlex has a description by Daniel Roth about how to use WebApi cross-domain JSON queries (a.k.a JSONP) using jQuery.

The "sample" he references can be found in the WCF CodePlex repository here. It is in the "default" folder.

Also, make sure you install the WebApiEnhancements for Preview 6 using NuGet otherwise none of this will work.

You'll need a Global.asax.cs with something like the following...

public class Global : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        var config = new WebApiConfiguration() { EnableTestClient = true };
        RouteTable.Routes.MapServiceRoute<HelloWorldApi>("api", config);
    }
}

The other key is to account for an "extension" in your URI template...

[WebGet(UriTemplate="hello{ext}")]

Then you make your jQuery call like this...

$.getJSON("/api/hello.jsonp?callback=?", function (data) {
    $("div").html(data);
}); 

Here's another blog post that describes how to add a JsonpFormatter to a project.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top