json和asp.net的跨域问题 [英] cross domain problem with json and asp.net

查看:93
本文介绍了json和asp.net的跨域问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务器(其他域)访问数据.

我在服务器中创建了一个名为hellojson.asmx的Web服务.

例如:www.example.com/services/JsonWeb.asmx

Web服务代码:

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



在本地主机中,我创建了一个名为 login.html
的html文件.



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>


javascript文件


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

推荐答案

.ajax({ contentType:" , 网址:" , dataType:' jsonp', 成功:函数(json){ // 用json(在这种情况下为数组)做事 alert(" ); }, 错误:函数(xmlHttpRequest,textStatus,errorThrown){ 警报(错误抛出); } });
.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..


看起来像


.getJSON在这种情况下会更有帮助.引用此线程- http://stackoverflow.com/a/2383363/312219 [ http://stackoverflow.com/questions/2414899/why-isnt-jquery-automatically-appending-the-jsonp-callback [ ^ ].这两个链接可能会对您有所帮助.

更新:我突然想起在jsfiddle小提琴中看到了类似这样的远程调用.

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

希望这会有所帮助!
.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!


这篇关于json和asp.net的跨域问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆