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

}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
@KrishnaMohan, for more details you can check it out this link stackoverflow.com/questions/14221429/… Let me know if you still have issues.
i didn't get anything by using your example. even it is not going into error function also please find the below code
@KrishnaMohan, Had you set headers for cross-origin-allow ? If not then please set that first & then check it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.