利用$.getJSON() 跨域请求操作
原文发布时间为:2011-01-14 —— 来源于本人的百度文章 [由搬家工具导入]
$.get 没有权限? $.post 没有权限? 因为他们都不能跨域,那就用 $.getJSON() 吧
利用$.getJSON() 跨域调用aspx、ashx和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() 跨域请求操作的更多相关文章
- 用 jQuery.getJSON() 跨域请求 JSON 数据
$.getJSON()可以理解为特殊形式的$.ajax(),手册里的说明好复杂,这里只记录一下用到的跨域请求. 先说在同一域名下,js发送数据到php,php返回JSON数据: $.getJSON(' ...
- jquery $.getJSON()跨域请求
以前总是没搞明白是怎么回事,现在是迫不得已,就仔细看了看说明文档,终于测试成功了,记下 1,同一域名下和其他的请求可以是一样的 js: 代码如下: var url="http://loc ...
- ajax跨域请求的问题
使用getJson跨域请求,需要向服务器发送一个参数callback=? $.getJSON("http://appcenter.mobitide.com/admin/appSearch.p ...
- 利用Nginx轻松实现Ajax的跨域请求(前后端分离开发调试必备神技)
利用Nginx轻松实现浏览器中Ajax的跨域请求(前后端分离开发调试必备神技) 前言 为什么会出现跨域? 造成跨域问题的原因是因为浏览器受到同源策略的限制,也就是说js只能访问和操作自己域下的资源,不 ...
- jQuery异步请求(如getJSON)跨域解决方案
相信大家在使用jQuery异步请求非自己网站内相对资源(通过别人站点上的URL直接读取)使经常会遇到如下错误吧,实际上这些错误都是浏览器安全机制“搞的鬼”,才让我们开发路上遇到了拦路虎. 当你直接在浏 ...
- Ajax_05之跨域请求
1.跨域请求: Cross Domain Request:跨域名的HTTP请求,浏览器从某个域名下的资源访问了另一域名下的另一资源(协议.域名或是端口号不同): ①浏览器允许跨域请求的情形: < ...
- AJAX 跨域请求 - JSONP获取JSON数据
Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流行术语为 Web 2.0 站点)的关键技术.Ajax 允许在不干扰 Web 应用程序的显示 ...
- jQuery ajax跨域请求的解决方法
在Ajax应用中,jQuery的Ajax请求是非常容易而且方便的,但是初学者经常会犯一个错误,那就是Ajax请求的url不是本地或者同一个服务器下面的URI,最后导致虽然请求200,但是不会返回任何数 ...
- Ajaxadr ajax跨域请求crossdomain
最近工作需要用到ajax跨域请求参数,网上找很很久,最终得到解决之道.分享一下吧,希望能帮到各位 也许你已经发现在浏览器直接敲路径能获得对方提供接口的参数,而一到项目中Ajax请求却老是失败.原因是, ...
随机推荐
- 记python版本管理--pyenv
随记: 众所周知,python2.x版本与3.x版本有比较大的区别,如果你是2.x版本的使用者,突然接了3.x版本的项目,或者反过来,遇到这种情况该怎么办呢?重新安装自己电脑上的python,来匹配对 ...
- Linux ps与top命令
Linux ps与top命令 这两个命令都是查看系统进程信息的命令,但是用处有点儿不同 1.ps命令--提供系统过去信息的一次性快照 也就是说ps命令能够查看刚刚系统的进程信息 命令:ps aux或 ...
- nodeJS 服务端文件上传
var http = require('http'); var path = require('path'); var fs = require('fs'); function uploadFile( ...
- 四 python并发编程之协程
一 引子 本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去 ...
- Python基础学习总结__Day1
一.Python是一门什么类型语言 1.解释型:一边编译一边执行,劣势是运行速度慢,但通过运用PyPy交互解释器(JIT技术)会让python程序执行速度快很多.优势是可移植性强. 2.强类型:即类型 ...
- CRM第一篇:权限组件之权限控制
一.权限组件(1):一级菜单 二.权限组件(2):二级菜单 三.权限组件(3):默认选中非菜单(二级菜单) 四.权限组件(4):给动态菜单增加面包屑导航 五.权限组件(5):权限粒度控制到按钮 六.权 ...
- C语言中strtod()函数的用法详解
函数原型: #include <stdlib.h> double strtod(const char *nptr, char **endptr); C语言及C++中的重要函数. 名称含义 ...
- momo不是玩具,.Net雄起
互联网时代 .NET 会渐渐衰落吗?一个架构师对 .NET 的思考 2015-12-14 11:03 darklx 博客园 字号:T | T 为了更好的适应互联网时代的需求,我们公司已经把我们的 .N ...
- datagrid的基本属性&查询和清空功能的实现
1.datagrid基本属性 <script charset=UTF-8"> $(function(){ $("#datagrid").datagrid({ ...
- idea 占用内存优化调整
idea 占用内存优化调整 https://www.cnblogs.com/metoy/p/5967696.html https://blog.csdn.net/zdxxinlang/arti ...