none
Can't get a Javascript to call a wcf webservice on localhost or on a crm 2013 server RRS feed

  • Question

  • I'm trying to write a test web service for CRM 2013 that will fire from an iframe on a form and call an AJAX enabled WCF webservice sitting on the same crm server. The message will need to send a POST (not get) message to the service. The service seems to be returning a "400" error: "XMLHttpRequest cannot load http://localhost/PasswordResetter/Resetter.svc. Invalid HTTP status code 400". I've tried browsing through 30 or so "Tutorials" and explanations from here, the web, the msdn, and I have no idea what I seem to be doing wrong or why it's not working. I couldn't get it to work from the iframe, nor from a custom html page where the service and html page I created were both on localhost.

    Lastly, if this question is somehow "improperly formed" for this site, please point out to me how/why so I may modify it appropriately. Thank you in advance for all of your help.

    Below is my Interface:

        namespace PasswordResetter
        {
            // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IResetter" in both code and config file together.
            [ServiceContract]
            public interface IResetter
            {
                [OperationContract()]
                string test(string a, string b);
            }
        }

    Below is the method:

        public class Resetter : IResetter
        {
            [WebInvoke(Method = "POST", //was POST
            BodyStyle = WebMessageBodyStyle.Bare, //This means that our JSOn will map directly to our parameters with no additional processing??
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "/Test")]
            public string test(string a, string b)
            {
                //return "Success!: " + a + " " + b;
                string myJsonString = (new JavaScriptSerializer()).Serialize("Hello World! A: " + a + " b: " + b);
                return myJsonString;
            }
        }

    Here's the Javascript I'm trying to get working:

    $(document).ready(function () {
            $('#iframesubmit').click(function () {
                //var obj = { username: $("#txtuser").val(), name: $("#txtname").val() };
    var obj = '[{ username: $("#password_old").val(), name: $("#password_new").val() }]';
                $.ajax({
                    type: "POST", //was POST
                    contentType: "application/json; charset=utf-8",
                    url: "http://localhost/PasswordResetter/Resetter.svc",
                    //data: "",
    data: JSON.stringify(obj),
    //data: "{ 'a': '" + $("#password_old").val() + "', 'b': '" + $("#password_new").val() + "'}",//JSON.stringify(obj),
                    dataType: "json",
                    success: function (data) {
                        alert("Successfully register");
    document.getElementById("results").value = "Response: " + data; //service.responseText;
                        $("#iframesubmit").click();
                    },error: function (xhr)
                    {
                        window.alert('error: ' + xhr.statusText);
                    }
                });
            });
        });

    And finally my web.config:

    `    <?xml version="1.0"?>
        <configuration>
        <appSettings>
            <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
          </appSettings>
          <system.web>
            <compilation debug="true" targetFramework="4.5" />
            <httpRuntime targetFramework="4.5"/>
          </system.web>
          <system.serviceModel>   
            <bindings>
              <webHttpBinding>
            <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true">
              <security mode="Transport">
                <transport clientCredentialType="None"></transport>
              </security>
            </binding>
          </webHttpBinding>
          <customBinding>
            <binding name="CRMPoint.CRM.Services.PasswordResetter.customBinding0">
              <!--  <binaryMessageEncoding /> -->
              <httpTransport />
            </binding>
          </customBinding>
        </bindings>
        <services>
          <service name="PasswordResetter">
            <endpoint address="http://localhost/PasswordResetter/Resetter.svc" behaviorConfiguration="PasswordResetter.Service1AspNetAjaxBehavior"
              binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP"
              contract="PasswordResetter.IResetter" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
          <service name="Test">
            <endpoint address="http://localhost/PasswordResetter/Resetter.svc" binding="customBinding"
              bindingConfiguration="CRMPoint.CRM.Services.PasswordResetter.customBinding0"
              contract="PasswordResetter" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
          <service name="PasswordResetter.Service1">
            <endpoint address="http://localhost/PasswordResetter/Resetter.svc" behaviorConfiguration="PasswordResetter.Service1AspNetAjaxBehavior"
              binding="webHttpBinding" contract="PasswordResetter.Service1" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="webHttpBehavior">
              <webHttp helpEnabled="true" />
            </behavior>
            <behavior name="PasswordResetter.Service1AspNetAjaxBehavior">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <protocolMapping>
          <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
          multipleSiteBindingsEnabled="true" />
        <standardEndpoints>
          <webScriptEndpoint>
            <standardEndpoint name="test" crossDomainScriptAccessEnabled="true" />
          </webScriptEndpoint>
        </standardEndpoints>
        </system.serviceModel>
        <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Content-Type" />
          </customHeaders>
        </httpProtocol>
        <directoryBrowse enabled="true"/>
        </system.webServer>
        </configuration>
    `

    Friday, February 13, 2015 10:49 PM

Answers

  • Hi joshalcorn22,

    I see that you have used the following:

    <endpointBehaviors>
             <behavior name="webHttpBehavior">
               <webHttp helpEnabled="true" />
             </behavior>
             <behavior name="PasswordResetter.Service1AspNetAjaxBehavior">
               <enableWebScript />
             </behavior>
    </endpointBehaviors>

    But it seems that you do not apply these endpoint behaviors to the endpoint, please try to apply it. After that if it still can not work, please try to change the POST method to the GET method to see if you can get the right result in the browser. If you can get the result with the GET method, then it may have something wrong with your javascript code. If you can not get the result with the GET method, then please try to enable the WCF trace to help you find the root cause.

    #How to enable WCF Trace:
    http://www.codeproject.com/Articles/420538/Simple-steps-to-enable-tracing-in-WCF .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 5:51 AM
    Moderator

All replies

  • url: "http://localhost/PasswordResetter/Resetter.svc",

    Maybe localhost means nothing at this point and you have to give the locahost IP, which is 127.0.0.1.

    I don't know.....

    Saturday, February 14, 2015 4:40 AM
  • Hello Darnold924. Thank you for the suggestion, however it didn't work. 
    Monday, February 16, 2015 3:11 PM
  • Hi joshalcorn22,

    I see that you have used the following:

    <endpointBehaviors>
             <behavior name="webHttpBehavior">
               <webHttp helpEnabled="true" />
             </behavior>
             <behavior name="PasswordResetter.Service1AspNetAjaxBehavior">
               <enableWebScript />
             </behavior>
    </endpointBehaviors>

    But it seems that you do not apply these endpoint behaviors to the endpoint, please try to apply it. After that if it still can not work, please try to change the POST method to the GET method to see if you can get the right result in the browser. If you can get the result with the GET method, then it may have something wrong with your javascript code. If you can not get the result with the GET method, then please try to enable the WCF trace to help you find the root cause.

    #How to enable WCF Trace:
    http://www.codeproject.com/Articles/420538/Simple-steps-to-enable-tracing-in-WCF .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 5:51 AM
    Moderator