none
JSONP sample - JsonpAjaxService is not defined RRS feed

  • Question

  • I am trying to call the Jsonp example from my website (c:\WR-WCF-Samples\WCF\Basic\Ajax\JSONP).

    Firebug indicates that I have a

    ReferenceError: JsonpAjaxService is not defined
    var proxy= new JsonpAjaxService.CustomerService();

    I placed the script and body from JSONPClientPage.aspx into my webpage, changing the ServiceReference Path to

                <asp:ServiceReference Path="http://192.168.64.180/Service_deploy/service.svc" />

    I added service and behavior definitions to Web.Config:

    <system.serviceModel>
        <services>
          <service name=" JsonpAjaxService" behaviorConfiguration="MyServiceBehavior">
            <endpoint address=""
                      binding="webHttpBinding"
                      contract="service"
              behaviorConfiguration="web" />
          </service>
        </services>
        <behaviors>

          <serviceBehaviors>
            <behavior name="MyServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="web">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
      </system.serviceModel>

    I built the solution in Visual Studio 2012 and then published the service to IIS.

    I assume that this is a Service/IIS/Website configuration issue, but I don't know what to change.  How does my webpage connect to JsonpAjaxService?

    Thanks so much for your help!
    Saturday, May 4, 2013 12:56 PM

Answers

  • Hi,

    I tested on my side, after publish the service in this sample to IIS and try to call the service from a website, it works as expected. As you can notice that it use the WebScriptEndpoint here that with a fixed WebHttpBinding binding that automatically adds the WebScriptEnablingBehavior behavior, you do not need add another endpoint with webHttpBinding binding.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 8, 2013 2:10 AM
    Moderator
  • I figured it out.  Thanks SOO much for your help. 

    $.getJSON( 'http://192.168.64.180/jsonp0513/service.svc/GetCustomer?callback=?', null, function(res){

    $("#response").html("it worked");
    console.log(res);              // log the result from the callback
     $.each(res, function(key, val) {
    $("#response1").html(key);
    $("#response2").html(val);

    I needed to add the callback=? so it knew that it was jsonp, and then I needed to parse the response.


    Peggy Thrasher

    Wednesday, May 15, 2013 9:12 PM

All replies

  • Hi,

    I afraid the service is not properly published or not started, please make sure the IIS server is running, and you can test by browsing the service in browser to check if you have publish the service properly on IIS.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 7, 2013 10:09 AM
    Moderator
  • Thanks for the help.  I can now browse the service, but it tells me that
    Metadata publishing for this service is currently disabled.

    I've tried adding a base address, but it is still not happy:

    <?xml version="1.0"?>
    <!-- Copyright (c) Microsoft Corporation.  All rights reserved. -->
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <authentication mode="None" />
      </system.web>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
      <system.serviceModel>
        <services>
          <service name="Service_deploy.service" behaviorConfiguration="ServiceBehavior">
            <host>
               <baseAddresses>
                  <add baseAddress="http://localhost/Service_deploy/service.svc"/>
               </baseAddresses>
            </host>
            <endpoint address="http://localhost/Service_deploy/service.svc"
                  binding="webHttpBinding"
                  contract="IService"
                  behaviorConfiguration="web" />
            <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="ServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="web">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
        <standardEndpoints>
          <webScriptEndpoint>
            <standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
          </webScriptEndpoint>
        </standardEndpoints>
      </system.serviceModel>
    </configuration>


    Peggy Thrasher

    Tuesday, May 7, 2013 6:13 PM
  • Hi,

    I tested on my side, after publish the service in this sample to IIS and try to call the service from a website, it works as expected. As you can notice that it use the WebScriptEndpoint here that with a fixed WebHttpBinding binding that automatically adds the WebScriptEnablingBehavior behavior, you do not need add another endpoint with webHttpBinding binding.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 8, 2013 2:10 AM
    Moderator
  • Thank you for helping me.  I am a software developer who has no experience with iis, Visual Studio and web services.  I started back with the original example, and published the service.  I put localhost/test in my browser and I see the listing:

    bin

    service.svc

    web.config

    When I choose service.svc it tells me that Metadata publishing for this service is currently disabled, along with a series of lines to add to the web.config for the service. 

    I added the behavior configuration, and the service with endpoint (though I don't know that I got the values for MyNamespace or MyServiceType correct.  I also do not know whether I have a problem with:

    Note: the service name must match the configuration name for the service implementation

    Note: your service must have an http base address to add this endpoint

    This is the web.config as it stands now.

    <?xml version="1.0"?>
    <!-- Copyright (c) Microsoft Corporation.  All rights reserved. -->
    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <authentication mode="None" />
      </system.web>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
      <system.serviceModel>
        <services>
          <!-- Note: the service name must match the configuration name for the service implementation. -->
          <service name="test.service" behaviorConfiguration="MyServiceTypeBehaviors" >
            <!-- Add the following endpoint.  -->
            <!-- Note: your service must have an http base address to add this endpoint. -->
            <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
              <serviceMetadata httpGetEnabled="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
        <standardEndpoints>
          <webScriptEndpoint>
            <standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
          </webScriptEndpoint>
        </standardEndpoints>
      </system.serviceModel>
    </configuration>

    This still gives me "Metadata publishing for this service is currently disabled.

    What else do I need to change?


    Peggy Thrasher

    Wednesday, May 8, 2013 3:54 PM
  • Hi,

    REST service doesn't use metadata. Metadata (Mex endpoint) are only for SOAP services. Check the discusstion from:

    http://stackoverflow.com/questions/5035118/exposing-meta-data-for-a-wcf-4-0-rest-template-service

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, May 11, 2013 12:06 PM
    Moderator
  • The reason I had added the Mex endpoint is that when I take the example, change nothing, build and publish it and then go through a browser to the service it says: 

    This is a Windows© Communication Foundation service.

    Metadata publishing for this service is currently disabled.

    If you have access to the service, you can enable metadata publishing by completing the following steps to modify your web or application configuration file:

    1. Create the following service behavior configuration, or add the <serviceMetadata> element to an existing service behavior configuration:

    <behaviors>
     <serviceBehaviors>
     <behavior name="MyServiceTypeBehaviors" >
     <serviceMetadata httpGetEnabled="true" />
     </behavior>
     </serviceBehaviors>
    </behaviors>
    

    2. Add the behavior configuration to the service:

    <service name="MyNamespace.MyServiceType" behaviorConfiguration="MyServiceTypeBehaviors" >
    

    Note: the service name must match the configuration name for the service implementation.

    3. Add the following endpoint to your service configuration:

    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    

    Note: your service must have an http base address to add this endpoint.

    I'm happy to not use metadata, but I don't know how to configure it so that it doesn't complain.

    Thanks for helping!


    Peggy Thrasher

    Monday, May 13, 2013 7:04 PM
  • Hi,

    As I mentioned above, metadata is not available for Restful service, so it(Metadata publishing for this service is currently disabled) is a expected behavior when you are trying to browser the metadata of the service.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 15, 2013 1:55 AM
    Moderator
  • OK!  This is starting to sink in.  Thank you for your patience with me.

    I am now at the point where I can put

    http://192.168.64.180/jsonp0513/service.svc/GetCustomer

    in my browser and get back

    {"d":{"__type":"Customer:#Microsoft.Samples.Jsonp","Address":"1 Example Way","Name":"Bob"}}

    When I try to call the service from my webpage, though something doesn't work.  The call in my webpage looks like this:


    <script type="text/javascript">

    function myFunction() {

    $("#response").html("ready to get json");

     $.getJSON( 'http://192.168.64.180/jsonp0513/service.svc/GetCustomer', null, function(res){

    $("#response").html(res);
    console.log(res);              // log the result from the callback
    });
    };


    </script>

    <p id="response">Old Text</p>
    <button type="button" onclick="myFunction()">Click Me!</button>


    When I click the button I do see the text change to "ready to get json", but I am expecting the text to then change to
    {"d":{"__type":"Customer:#Microsoft.Samples.Jsonp","Address":"1 Example Way","Name":"Bob"}}
    or something like that.

    Is res a string?  Do I need to parse it?

    I even tried to just put a different text message in ( $("#response").html("this worked"); ) but that didn't work either.  So I think I'm not getting into the function.


    Peggy Thrasher

    Wednesday, May 15, 2013 8:25 PM
  • I figured it out.  Thanks SOO much for your help. 

    $.getJSON( 'http://192.168.64.180/jsonp0513/service.svc/GetCustomer?callback=?', null, function(res){

    $("#response").html("it worked");
    console.log(res);              // log the result from the callback
     $.each(res, function(key, val) {
    $("#response1").html(key);
    $("#response2").html(val);

    I needed to add the callback=? so it knew that it was jsonp, and then I needed to parse the response.


    Peggy Thrasher

    Wednesday, May 15, 2013 9:12 PM