原文发布时间为: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/\WebServices\Service.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>

利用$.getJSON() 跨域请求操作的更多相关文章

  1. 用 jQuery.getJSON() 跨域请求 JSON 数据

    $.getJSON()可以理解为特殊形式的$.ajax(),手册里的说明好复杂,这里只记录一下用到的跨域请求. 先说在同一域名下,js发送数据到php,php返回JSON数据: $.getJSON(' ...

  2. jquery $.getJSON()跨域请求

    以前总是没搞明白是怎么回事,现在是迫不得已,就仔细看了看说明文档,终于测试成功了,记下   1,同一域名下和其他的请求可以是一样的 js: 代码如下: var url="http://loc ...

  3. ajax跨域请求的问题

    使用getJson跨域请求,需要向服务器发送一个参数callback=? $.getJSON("http://appcenter.mobitide.com/admin/appSearch.p ...

  4. 利用Nginx轻松实现Ajax的跨域请求(前后端分离开发调试必备神技)

    利用Nginx轻松实现浏览器中Ajax的跨域请求(前后端分离开发调试必备神技) 前言 为什么会出现跨域? 造成跨域问题的原因是因为浏览器受到同源策略的限制,也就是说js只能访问和操作自己域下的资源,不 ...

  5. jQuery异步请求(如getJSON)跨域解决方案

    相信大家在使用jQuery异步请求非自己网站内相对资源(通过别人站点上的URL直接读取)使经常会遇到如下错误吧,实际上这些错误都是浏览器安全机制“搞的鬼”,才让我们开发路上遇到了拦路虎. 当你直接在浏 ...

  6. Ajax_05之跨域请求

    1.跨域请求: Cross Domain Request:跨域名的HTTP请求,浏览器从某个域名下的资源访问了另一域名下的另一资源(协议.域名或是端口号不同): ①浏览器允许跨域请求的情形:  < ...

  7. AJAX 跨域请求 - JSONP获取JSON数据

    Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流行术语为 Web 2.0 站点)的关键技术.Ajax 允许在不干扰 Web 应用程序的显示 ...

  8. jQuery ajax跨域请求的解决方法

    在Ajax应用中,jQuery的Ajax请求是非常容易而且方便的,但是初学者经常会犯一个错误,那就是Ajax请求的url不是本地或者同一个服务器下面的URI,最后导致虽然请求200,但是不会返回任何数 ...

  9. Ajaxadr ajax跨域请求crossdomain

    最近工作需要用到ajax跨域请求参数,网上找很很久,最终得到解决之道.分享一下吧,希望能帮到各位 也许你已经发现在浏览器直接敲路径能获得对方提供接口的参数,而一到项目中Ajax请求却老是失败.原因是, ...

随机推荐

  1. springboot中加入druid对sql进行监控

    springboot作为现在十分流行的框架,简化Spring应用的初始搭建以及开发过程,现在我们就使用springboot来进行简单的web项目搭建并对项目sql进行监控. 项目的搭建就省略了,spr ...

  2. PKI

    公钥基础设施(Public Key Infrastructure,简称PKI)是眼下网络安全建设的基础与核心,是电子商务安全实施的基本保障,因此,对PKI技术的研究和开发成为眼下信息安全领域的热点.本 ...

  3. thinkphp 3.2.3 - Route.class.php 解析(路由匹配)

    class Route { public static function check(){ $depr = C('URL_PATHINFO_DEPR'); // '/' $regx = preg_re ...

  4. JZOJ 4421. aplusb

    4421. aplusb Time Limits: 1000 ms  Memory Limits: 524288 KB  Detailed Limits   Goto ProblemSet Descr ...

  5. 水题:HDU1034-Candy Sharing Game

    解题心得: 1.我是用的模拟的算法直接模拟的每一轮的分配方法的得出的答案,看到有些大神使用的链表做的,好像链表才是这道题的镇长的做法吧. 题目: Candy Sharing Game Time Lim ...

  6. eclipse中设置JVM内存

    一.   修改jdk 使用内存: 找到eclispe 中window->preferences->Java->Installed JRE ,点击右侧的Edit 按钮,在编辑界面中的 ...

  7. Contest - 中南大学第六届大学生程序设计竞赛(Semilive)

    题1:1160十进制-十六进制 注意他给的数据范围 2^31,int是 2^31-1 #include<iostream> using namespace std; int main() ...

  8. Logistic回归python实现小样例

    假设现在有一些点,我们用一条直线对这些点进行拟合(该线称为最佳拟合直线),这个拟合过程就称作回归.利用Logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,依次进行分类.Lo ...

  9. mysql基础查询

    #进阶1:基础查询/*语法:select:查询列表 from 表名; 类似于:System.out.println(打印的东西); 特点:1.查询列表可以是:表中的字段.常量值.表达式.函数2.查询的 ...

  10. sql server 不可见字符处理 总结

    前言 问题描述:在表列里有肉眼不可见字符,导致一些更新或插入失败. 几年前第一次碰见这种问题是在读取考勤机人员信息时碰见的,折腾了一点时间,现在又碰到了还有点新发现就顺便一起记录下. 如下图所示 go ...