Click here to Skip to main content
12,067,652 members (50,882 online)
Rate this:
 
Please Sign up or sign in to vote.
I am trying to access the data from server(other domain).

I have a created a Webservice called hellojson.asmx which is in server

Ex: www.example.com/services/JsonWeb.asmx

Webservice code:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString.ToString());
            string status = "";
            string userid = "";
            Dictionary<string,> oDict = new Dictionary<string,>();
            SqlCommand com = new SqlCommand("select userid from users where username='" + x + "' and password='" + y + "'", con);
 
            SqlDataReader dr;
            if (con.State == ConnectionState.Closed)
                con.Open();
            dr = com.ExecuteReader();
 
            if (dr.Read())
            {
                //userid = dr["UserId"].ToString();            
                dr.Close();
                con.Close();
                status = "Login Success";
                SqlDataAdapter da = new SqlDataAdapter("select name from Employee", con);
                DataSet ds = new DataSet();
                da.Fill(ds,"employees");
 
                object[] arr = new object[ds.Tables["employees"].Rows.Count + 1];
 
                for (int i = 0; i <= ds.Tables["employees"].Rows.Count - 1; i++)
                {
                    arr[i] = ds.Tables["employees"].Rows[i].ItemArray;
                }
 
                oDict.Add("employees", arr);
                
            }
            else
            {
                dr.Close();
                con.Close();
                status = "Login failed";
            }
          
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(status);

In Local host i have created a html file called login.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Login Page</title>
 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>
 
        <form method="get" action="#">
                <label for="Username"><em>* </em> Username: </label>
                <input type="text" id="Lusername" name="Username" class="required" />
 
                <label for="password"><em>* </em>Password: </label>
                <input type="password" id="Lpassword" name="password" class="required" />
 
            <div class="ui-block-a"><button data-theme="c">Cancel</button></div>
            <div class="ui-block-b"><button type="submit" data-theme="b">Submit</button></div>
</body>
</html>
In the javascript file

$.ajax({
                contentType: "application/json; charset=utf-8",
                url: "www.example.com/services/JsonWeb.asmx",
                    dataType: 'jsonp',
                    success:function(json){
                    // do stuff with json (in this case an array)
                    alert("Success");
                    },
                    error: function(xmlHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                    }
                    });
I would like to display the result in alert box..but i am unable get the result pls help me..
Posted 12-Jun-12 0:41am
Edited 12-Jun-12 3:42am
v2
Comments
Karthik. A 12-Jun-12 22:20pm
   
Looks like you are using the correct dataType parameter. What is the error thrown? Can you update / comment w/ the error that's thrown?
tanisha_forum 12-Jun-12 22:55pm
   
Hi karthik thanks for your reply..When i am using jsonp datatype its displaying an error "jquery 1234234345756544767 was not called". if i change the datatype as json instead of jsonp its showing an empty alert box..pls fix the problem what i am missing there
Karthik. A 12-Jun-12 23:10pm
   
I have added an answer, check it out...

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Looks like $.getJSON will be more helpful in this case. Refer this thread - http://stackoverflow.com/a/2383363/312219[^] and this thread http://stackoverflow.com/questions/2414899/why-isnt-jquery-automatically-appending-the-jsonp-callback[^]. These 2 links might help you.

Update: I suddenly remembered seeing something like this, remote call here in this jsfiddle fiddle.

http://jsfiddle.net/pborreli/pJgyu/[^], This uses the following jquery plugin: https://raw.github.com/kerberoS/jQuery-Tweets/master/js/jquery.tweets.0.1.js[^]

Hope this helps!
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 12 Jun 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100