利用$.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请求却老是失败.原因是, ...
随机推荐
- 自封装的AJAX
/* * *create by royal in 2019/1/23 * *royalAjax 自封装ajax函数 * *paramsObj JSON类型参数 *require params: * t ...
- 问题:Could not install packages due to an EnvironmentError: [Errno 13] Permission denied:
1.安装django 执行pip3 install --user django 2.解决方法:加--user 执行pip3 install --user django
- 绘制三角形:imageline()
<?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...
- MySQL存储引擎MyISAM与InnoDB的区别比较
使用MySQL当然会接触到MySQL的存储引擎,在新建数据库和新建数据表的时候都会看到. MySQL默认的存储引擎是MyISAM,其他常用的就是InnoDB了. 至于到底用哪种存储引擎比较好?这个问题 ...
- 42.VUE学习之--组件之子组件使用$on与$emit事件触发父组件实现购物车功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II
[译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...
- java 调用第三方系统时的连接代码-记录
前言:该文章主要是总结我在实际工作中遇到的问题,在调取第三方系统的时候出现的问题,算自己的总结.各位博友如果有什么建议或意见欢迎留言指正. 先将准备传入参数 再与第三方系统建立连接 再第三方系统处理后 ...
- 用python给图片添加半透明水印
# coding:utf-8 from PIL import Image, ImageDraw, ImageFont def add_text_to_image(image, text): font ...
- J.U.C 系列之 Tools
JDK 5.0 开始,java并发大师Doug lea 为我们带来了更高效更应用的java并发API Java.util.concurrent包,简称J.U.C J.U.C 体系由如下五个知识结构构成 ...
- nable to execute dex: Multiple dex files define Lcom/chinaCEB/cebActivity/R
用proguaid 只混淆Android项目的src下的包的话,如果出现了上面的问题: nable to execute dex: Multiple dex files define Lcom/chi ...