title: Call jquery ajax "POST" with crossdomain jsonp from C# ASP.NET asmx
body: <pre><code>$.ajax({type: "post",
              url: "http://test.com/Register",
              data: {
              "UserID": '12',
              "FirstName": 'FN1'
              },
  
              accept: {
                  json: "application/json",
                  jsonp: "application/javascript"
              },
              dataType: "jsonp",
              crossDomain: true,
              beforeSend: function (xhr) {
                  xhr.withCredentials = true;
              },
              xhrFields: {
                  withCredentials: true
              },
              cache: false,
              success: function (data) {
                  if(data.success)
          {
              alert("success");
          }
          else
          {
              alert("failed");
          }   
              }
  });
  </code></pre>
  
  <p>I have this Ajax call working with javascript.
  But I'm trying to make this same call through C#, using HTTPWebRequest and am not getting the same results.
  When I do the call through C# it returns a webpage, instead of a json string,which is not right. And that is happening because the call for some reason is not proper.
  But the AJAX call return a proper json string back to me.
  I think it is because the ajax call specifies the datatype as "jsonp" and specifies it is cross domain.</p>
  
  <p>The reason I need this to work in C# is because I want this C# call to be made inside a web service.
  So a form calls my web service(MyWebservice.asmx), which inturn makes this call in question to <a href="http://test.com/Register" rel="nofollow">http://test.com/Register</a>.
  So I cant really have AJAX / Jquery written into my webservice, and would rather have to use C# to make this call.</p>
  
  <p>Any help?</p>
  
  <pre><code>    string data = "[SOME JSON DATA]";
      byte[] bytesArray = encoding.GetBytes(data);
              HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://test.com/Register");
  
  
              httpRequest.Method = "POST";
              httpRequest.ContentType = "application/x-www-form-urlencoded";
              httpRequest.ContentLength = bytesArray.Length;
              using (Stream stream = httpRequest.GetRequestStream())
              {
                  stream.Write(bytesArray, 0, bytesArray.Length);
                  stream.Close();
              }
  
              HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
  
              Stream responseStream = response.GetResponseStream();
              StreamReader reader = new StreamReader(responseStream);
              // Read the response content.
              responseFromServer = reader.ReadToEnd(); //This gives an HTML page as response
  </code></pre>