title: Call webservice on outside server from javascript using asp.net and C#
body: <p>I'm trying to test web service calls using an ASP.NET page that creates a form with username and password fields and a "Submit" button. (Both jQuery and the .js file I'm using are included in script tags in the head element.)</p>
  
  <p>The "Submit" button calls a function created in the C# code behind file that makes a call to a separate JavaScript file.</p>
  
  <pre><code>protected void mSubmit_Click(object sender, EventArgs eventArgs)
  {
      String authenticate = String.Format("Authentication(\"{0}\",\"{1}\");", this.mUsername.Text,this.mPassword.Text);
      Page.ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", authenticate, true);
  }
  </code></pre>
  
  <p>The JavaScript function, <code>Authenticate</code>, makes web service call, using jQuery and Ajax, to a different server, sending JSON parameters and expecting back JSON in response.</p>
  
  <pre><code>function Authentication(uname, pwd) {
  
      //gets search parameters and puts them in json format
      var params = '{"Header":{"AuthToken":null,"ProductID":"NOR","SessToken":null,"Version":1},"ReturnAuthentication":true,"Password":"' + pwd + '","Username":"' + uname + '",”ReturnCredentials”:false }';
  
      var xmlhttp = $.ajax({
          async: false,
          type: "POST",
          url: 'https://myHost.com/V1/Identity/Authenticate',
          data: params,
          contentType: 'application/json'
      });
  
      alert(xmlhttp.statusText);
      alert(xmlhttp.responseText);
  
      return;
  }
  </code></pre>
  
  <p>However, because the web service I'm calling is on a different server than the ASP.NET, C# and JavaScript files, I'm not getting a <code>statusText</code> or <code>responseText</code> alert. </p>
  
  <p>Somehow, nothing is being sent to the web service and I'm not getting anything back, not even an error.  I tried putting a function in the <code>beforeSend</code> attribute, but that didn't fire.  Is there a special way I need to handle calling an off-server web service?</p>
  
  <p><b>UPDATE!</b></p>
  
  <p>At the advice of jjnguy, Janie and Nathan, I'm now trying a server side call to the web service using HttpWebRequest.  Using some of jjnguy's code as well as code from <a href="https://stackoverflow.com/questions/575893/why-does-my-c-client-posting-to-my-wcf-rest-service-return-400-bad-request">this question</a>, I've come up with this.</p>
  
  <pre><code>public static void Authenticate(string pwd, string uname)
  {
      string ret = null;
  
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://myhost.com/V1/Identity/Authenticate");
      request.ContentType = "application/json";
      request.Method = "POST";
  
      string data = "{\"Header\":{\"AuthToken\":null,\"ProductID\":\"NOR\",\"SessToken\":null,\"Version\":1},\"ReturnAuthentication\":true,\"Password\":\"" + pwd + "\",\"Username\":\"" + uname + "\",\"ReturnCredentials\":false }'";
  
      byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
      request.ContentLength = byteData.Length;
  
      using (Stream postStream = request.GetRequestStream()) 
      {
          postStream.Write(byteData, 0, byteData.Length);
      }
  
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  
      using (response)
      {
          // Get the response stream  
          StreamReader reader = new StreamReader(response.GetResponseStream());
  
          // Console application output  
          ret = reader.ReadToEnd();
      }
  
      Console.WriteLine(ret);
  }
  </code></pre>
  
  <p>However, I'm getting a <code>(400) Bad Request</code> error from the remote server when I try to get the response from my HttpWebRequest.  The value of the Response property of the exception says <code>{System.Net.HttpWebResponse}</code> and the value of the Status property is <code>ProtocolError</code>.  I'm pretty sure this is because the URL is using HTTP SSL protocol. What can I do to get around that, other than having the ASP.NET page URL start with HTTPS (not an option)?</p>