You cant do direct service call to Web server from a jQuery application unless both your app and service is hosted in same service. For that you can add a proxy page to the project that can connect to service. Proxy can be an aspx page.
If Both Client and service in same server you can use code bellow:
var params = {
"Paramete1": Parameter1,
"Parameter2": Parameter2,
"Parameter3": Parameter3,
};
$.ajax({
type: "POST",
url: "http://50.57.145.165:8180/FTPEReport/ftpereports/fsreport?fsReport=",
async: true,
cache: false,
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify(params),
success: function (data) {
alert("Sucess");
},
error: function (xhr) {
alert("Error");
}
});
If Both Client and server is in different server then(I am here using .net framework to create proxy.) add a new aspx file in to project. From js call this aspx page through service call. Here the aspx page name is "ServiceCall.aspx"
var data ={
"Paramete1": Parameter1,
"Parameter2": Parameter2,
"Parameter3": Parameter3,
};
$.ajax({
cache: true,
type: "POST",
async: true,
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "ServiceCall.aspx?ServiceType= POST",
data: data,
success: function (data) {
alert("Sucess");
},
error: function (xhr) {
alert("Error");
}
});
});
add the following code in ServiceCall.Cs Page
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ServerCall : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public override void ProcessRequest(HttpContext context)
{
var data = context.Request;
var sr = new StreamReader(data.InputStream);
var stream = sr.ReadToEnd();
string type = HttpContext.Current.Request.QueryString["ServiceType"]; ;
try
{
NetworkCredential credential = new NetworkCredential("username", "password");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://50.57.145.165:8180/FTPEReport/ftpereports/fsreport?fsReport=");
httpWebRequest.Credentials = credential;
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Accept = "application/json";
if (type == "GET")
{
httpWebRequest.Method = WebRequestMethods.Http.Get;
var response = (HttpWebResponse)httpWebRequest.GetResponse();
using (var twitpicResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
HttpContext.Current.Response.Write(objText);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
else
{
httpWebRequest.Method = WebRequestMethods.Http.Post;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(stream);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var reader = new StreamReader(httpResponse.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
var objText = reader.ReadToEnd();
HttpContext.Current.Response.Write(objText);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
catch (Exception ex)
{
}
}
}
Hope this helps
Happy Coding :-)