Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
4.00/5 (1 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:
C#
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

XML
<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

JavaScript
$.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
Updated 12-Jun-12 2: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?
Developers Code 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

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!
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900