0

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);
}

}

Sooraj
  • 9,717
  • 9
  • 64
  • 99

1 Answers1

0

Your problem can be solve by JSONP. From Exactly What IS JSONP by Cameron Spear:

JSONP, which stands for "JSON with Padding" (and JSON stands for JavaScript Object Notation), is a way to get data from another domain that bypasses CORS (Cross Origin Resource Sharing) rules.

CORS is a set of "rules," about transferring data between sites that have a different domain name from the client.

Sample of JSONP usage:

$.ajax({
    url: "http://***.***.**.***/latlangs.asmx/Get_Lat_Longs",
    type: "POST",
    data: "{ }",
    dataType: "jsonp",
    jsonpCallback: "localJsonpCallback"
});

function localJsonpCallback(json) {
   //You can process response from here.
}

So You can use JSONP same as above in your case.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Darshan Jain
  • 479
  • 2
  • 10
  • Tank q Darshan jain. i updated the code as u said i didnt get anything event control not going to localJsonpCallback method also. can u help me to get out this problem – Krishna Mohan Apr 13 '16 at 12:07
  • @KrishnaMohan, for more details you can check it out this link http://stackoverflow.com/questions/14221429/how-can-i-produce-jsonp-from-an-asp-net-web-service-for-cross-domain-calls Let me know if you still have issues. – Darshan Jain Apr 13 '16 at 12:15
  • i didn't get anything by using your example. even it is not going into error function also please find the below code – Krishna Mohan Apr 14 '16 at 03:35
  • @KrishnaMohan, Had you set headers for cross-origin-allow ? If not then please set that first & then check it out. – Darshan Jain Apr 14 '16 at 04:30