I am trying to connect cross domain web services by using jquery along with asp.net web services. when I am trying to connect this I got error
message and this is working fine when it is in same localhost. But I want to get output when they are in different localhosts or in different posts , that means I want host my code in one machine and web service is in another machine.
here is my code.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://***.***.**.***/latlangs.asmx/Get_Lat_Longs",
crossDomain: true,
async: true,
data: "{ }",
dataType: "json",
success: function(data) {
alert("Success");
},
error: function(err) {
alert(err.statusText);
}
});
and here is my web service method or .asmx page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class LatLongs : System.Web.Services.WebService {
string con = @"Data Source=SERVER\QTTS;Initial Catalog=asdf;User ID=sa;Password=*****";
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string Get_Lat_Long_Values()
{
try
{
string stri = @"SELECT MAX(id) AS Expr1 FROM Temp_lat_long_values";
SqlDataAdapter daa = new SqlDataAdapter(stri, con);
DataTable dt = new DataTable();
daa.Fill(dt);
if (dt.Rows.Count > 0)
{
int id = Convert.ToInt32(dt.Rows[0][0].ToString());
stri = @"SELECT x,y,id from Temp_lat_long_values where id='" + id + "'";
daa = new SqlDataAdapter(stri, con);
DataTable dt1 = new DataTable();
daa.Fill(dt1);
if (dt1.Rows.Count > 0)
{
dt1.TableName = "Values";
DataSet ds = new DataSet();
ds.Tables.Add(dt1);
string daresult = DataSetToJSON(ds);
return daresult;
}
else
{
return "No data Found";
}
}
else
{
return "No data found";
}
}
catch (Exception e)
{
return e.Message;
}
}
public string DataSetToJSON(DataSet ds)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (DataTable dt in ds.Tables)
{
object[] arr = new object[1];
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
arr[0] = dt.Rows[i].ItemArray;
}
dict.Add(dt.TableName, arr);
}
JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(dict);
}
}