原文发布时间为: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. MySQL基础 - 1 数据库基础

    一.数据库基础 1.什么是数据库 1.数据库(database)是保存有组织的数据的容器( 通常是一个文件或一组文件 ) 2.数据库是一个以某种有组织的方式存储的数据集合 注意:数据库软件应该称为DB ...

  2. 第七篇:suds.TypeNotFound: Type not found: '(string, http://schemas.xmlsoap.org/soap/encoding/, )'

    想要用Python的suds模块调用webservice地址做自动测试,但是找了很多方法都失败了,最终找到另外一个模块可以作为客户端访问服务器地址. 1.针对非安全的http from zeep im ...

  3. Linux-SSH远程登陆

    SSH是什么 Secure Shell 安全外壳协议 建立在应用层基础上的安全协议 可靠.转为远程登陆会话和其他网络提供安全性的协议 SSH客户端是用于多种平台 服务器安装SSH服务 安装:yum i ...

  4. Elizabeth Taylor【伊丽莎白·泰勒】

    Elizabeth Taylor People fell in love with Elizabeth Taylor in 1944, when she acted in the movie Nati ...

  5. SpringMVC controller接收的中文参数乱码

    CharacterEncodingFilter只对POST请求有用,GET请求的需要对你运行的tomcat 目录conf/server.xml文件中<Connector connectionTi ...

  6. play后面加the不加the如何分辨

    play表示“参加(某种球类运动或棋牌类的活动)”时,不需要定冠词the,后面直接加球类运动名称或棋牌类活动名称,可根据实际情况翻译成“打,踢,下”等.例如: 1) He often plays fo ...

  7. Java-读取txt生成excel

    本段代码的目的是从txt文本中读取相应格式的数据,然后写入到对应格式的excel文档中 在敲本段代码的时候,也学习了一些其它知识点,如下: 1.byte[] b_charset= String.get ...

  8. jar包导入仓库中

    mvn install:install-file -Dfile=F:/kaptcha-2.3.jar -DgroupId=com.google.code.kaptcha -DartifactId=ka ...

  9. cf965c Greedy Arkady

    呸,大傻逼题,我更傻逼ref #include <iostream> using namespace std; typedef long long ll; ll n, k, m, d, a ...

  10. 编程高手解读什么是NodeJs?

    首先在搞清楚什么NodeJs之前,我们先来聊聊JavaScript,只要做过开发的人都应该知道JavaScript是目前最为流行的前端(客户端)脚 本语言,JavaScript在Web项目中的使用率可 ...