zoukankan      html  css  js  c++  java
  • 利用$.getJSON() 跨域请求操作

    原文发布时间为:2011-01-14 —— 来源于本人的百度文章 [由搬家工具导入]

    $.get 没有权限? $.post 没有权限? 因为他们都不能跨域,那就用 $.getJSON() 吧

    利用$.getJSON() 跨域调用aspxashx和WebServices

    ASP.NET2010-12-22 12:17:31阅读11评论0  字号: 订阅

      今天抽点时间对跨域访问进行学习了一下,原理就不介绍了,直接给出例子.      http://h.com   ------->请求http//x.com 域下的文件
      (1) http://h.com域下的文件 WebServices.htm  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>$.getJSON跨域调用WebServics</title>    <script src="../jQuery/jquery-1.4.4.js" type="text/javascript"></script></head><body>    <input type="button" id="btnGetMethod" value="跨域调用方法" />    <br />    <input type="button" id="btnGetServices" value="跨域调用WebServices" /></body></html><script type="text/javascript">$(function(){       var callback=function(){};  //客户端定义好的回调函数

        //(1) 跨域调用页面中的方法

    如果使用aspx的话,要把页面上面的HTML那块代码删除。

         $("#btnGetMethod").click(function(){        //(1) 直接使用$.getJSON方法,此种写法回调函数写成http://x.com/Aspx/test.aspx?callback=? 而不是http://x.com/Aspx/test.aspx?callback=callback形式          $.getJSON(             "http://x.com/Aspx/test.aspx?callback=?"     //GetJson是定义好的Web方法             ,{method:"getjsonp",UserName:"Linda",Pwd:"88888888"}             ,function(data){ alert(data.Use+"-->"+data.Password);          });        //(2)使用$.ajax()方法          $.ajax({              url:"http://x.com/Aspx/test.aspx"             ,dataType:"jsonp"             ,jsonp:"callback"             ,data:{method:"getjsonp",UserName:"Linda",Pwd:"987654321"}             ,success:function(data){                  alert(data.Use+"-->"+data.Password);             }          });               });    //(2) 下面是调用WebService    $("#btnGetServices").click(function(){           //(1) 直接使用$.getJSON方法          $.getJSON(             "http://x.com/WebServices/Service.asmx/GetJson?callback=?"     //GetJson是定义好的Web方法             ,{UserName:"Linda",Pwd:"1237777"}             ,function(data){ alert(data.name+"-->"+data.pwd);          });       //(2) 使用$.ajax()方法          $.ajax({             url:"http://x.com/WebServices/Service.asmx/GetJson?",             dataType:"jsonp",             jsonp:"callback",             data:{UserName:"Susan",Pwd:"123456789"},             success:function(data){                  alert(data.name+"-->"+data.pwd);             }          });    });  });</script>(2) http://x.com/Aspx/test.aspx 文件内容,这里只给出后台代码test.aspx.csusing System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;namespace Two.Aspx{    public partial class test : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            var callback = !String.IsNullOrEmpty(Request["callback"]) ? Request["callback"] : "";           var method = !String.IsNullOrEmpty(Request["method"]) ? Request["method"] : "";           var UserName = !String.IsNullOrEmpty(Request["UserName"]) ? Request["UserName"] : "";           var Pwd = !String.IsNullOrEmpty(Request["Pwd"]) ? Request["Pwd"] : "";           if (method != "")           {               var ReturnStr = callback + "(" + getjsonp(UserName, Pwd) + ")";               Response.Write(ReturnStr);               Response.End();           }        }        protected string getjsonp(string User,string Password)        {            return "{Use:'"+User+"',Password:'"+Password+"'}";        }    }}(3)  http://x.com/WebServicesService.asmx  页面内容
    using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Linq;
    namespace Two.WebServices{    /// <summary>    /// Service 的摘要说明    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    [ToolboxItem(false)]    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。   [System.Web.Script.Services.ScriptService]    public class Service : System.Web.Services.WebService    {
            [WebMethod]        public string HelloWorld()        {            return "Hello World";        }        [WebMethod]        public void GetJson(string UserName, string Pwd)        {           HttpRequest Request=HttpContext.Current.Request;          var callback =!string.IsNullOrEmpty(Request["callback"])?Request["callback"]:"";          HttpContext.Current.Response.Write(callback + "({name:'Dear" + UserName + "',pwd:'" + Pwd + "'})");          HttpContext.Current.Response.End();        }    }} 注: 如果是跨域请求WebServices的话,要在被请求的文件web.config文件下的<system web> 节点下配置如下内容   <webServices>      <protocols>        <add name="HttpPost"/>        <add name="HttpGet"/>      </protocols>    </webServices>
  • 相关阅读:
    ME05 黑匣子思维
    F06 《生活中的投资学》摘要(完)
    ME03 关于运气要知道的几个真相
    ME02 做一个合格的父母To be good enough parent
    ME02 认知之2017罗胖跨年演讲
    F03 金融学第三定律 风险共担
    F05 敏锐的生活,让跟多公司给你免单
    ML04 Accord 调用实现机器算法的套路
    D02 TED Elon Mulsk The future we're building — and boring
    ML03 利用Accord 进行机器学习的第一个小例子
  • 原文地址:https://www.cnblogs.com/handboy/p/7163965.html
Copyright © 2011-2022 走看看