Ajax跨域请求ashx文件与Webservice文件
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-1.7.1.js"></script>
<script type="text/javascript">
jQuery(function () {
jQuery.ajax({
url: "http://localhost:1203/Handler1.ashx?callback=?",
//jsonpCallback:callback,
dataType: "jsonp",
data: { name: "likong" },
success: function (result) {
window.alert("姓名:" + result.name + ", 性别:" + result.gender); } }) function callback(){
window.alert("回调成功!"); } })
</script>
</head>
<body>
</body>
</html>
ashx文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebApplication1
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
string callback = context.Request.QueryString["callback"];
var name = context.Request.QueryString["name"];
string json = "{\"name\":\"" + name + "\",\"gender\":\"" + "男" + "\"}";
//JSONP格式:回调函数名(json格式参数)
//括号后不要加分号
string result = callback + "(" + json + ")";
context.Response.ContentType = "application/json";
context.Response.Write(result); } public bool IsReusable
{
get
{
return false;
}
}
}
}
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-1.7.1.js"></script>
<script type="text/javascript">
jQuery(function () {
jQuery.ajax({
url: "http://localhost:1203/WebService1.asmx/GetGenderByName?callback=?",
//jsonpCallback:callback,
dataType: "jsonp",
data: { name: "李空" },
success: function (result) {
window.alert("姓名:" + ":" + result.name + ", 性别:" + result.gender);
} }); function callback(){
window.alert("回调成功!"); } })
</script>
</head>
</html>
或者:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-1.7.1.js"></script>
<script type="text/javascript">
jQuery(function () {
jQuery.getJSON("http://localhost:1203/WebService1.asmx/GetGenderByName?callback=?&name=李空", function (result) {
window.alert("姓名:" + ":" + result.name + ", 性别:" + result.gender);
}, callback()) function callback(){
window.alert("回调成功!"); } })
</script>
</head>
</html>
Webservice代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Data; namespace WebApplication1
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{ [WebMethod]
public void GetGenderByName(string callback, string name)
{
string json = "{\"name\":\"" + name + "\",\"gender\":\"" + "男" + "\"}";
string result = callback + "(" + json + ")";
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write(result);
HttpContext.Current.Response.End();
}
}
}
注释:测试Webservice是否调用成功,必须先运行Webservice页面!
Ajax跨域请求ashx文件与Webservice文件的更多相关文章
- 关于JQuery 中$.ajax函数利用jsonp实现Ajax跨域请求ASP.NET的WebService成功获取数据的案例
部署环境:Window 7 SP1+IIS7 成功方案: 其成功解决问题的几个重要因素如下: 1. 由于WebService默认不支持Get请求,所以要在Web.config配置文件内的& ...
- 为什么返回的数据前面有callback? ashx/json.ashx?的后面加 callback=? 起什么作用 js url?callback=xxx xxx的介绍 ajax 跨域请求时url参数添加callback=?会实现跨域问题
为什么返回的数据前面有callback? 这是一个同学出现的问题,问到了我. 应该是这样的: 但问题是这样的: 我看了所请求的格式和后台要求的也是相同的.而且我也是这种做法,为什么他的就不行呢? ...
- WebService跨域配置、Ajax跨域请求、附开发过程源码
项目开发过程中需要和其他公司的数据对接,当时我们公司提供的是WebService,本地测试,都是好的,Ajax跨域请求,就报错,配置WebService过程中,花了不少功夫,入不少坑,不过最终问题还是 ...
- 第113天:Ajax跨域请求解决方法
一.原生JS实现ajax 第一步获得XMLHttpRequest对象 第二步:设置状态监听函数 第三步:open一个连接,true是异步请求 第四部:send一个请求,可以发送一个对象和字符串,不需要 ...
- 浅谈linux 下,利用Nginx服务器代理实现ajax跨域请求。
ajax跨域请求对于前端开发者几乎在任何一个项目中都会用到,众所周知,跨域请求有三种方式: jsonp; XHR2 代理: jsonp: 这种应该是开发中是使用的最多的,最常见的跨域请求方法,其实aj ...
- 解决ajax跨域请求 (总结)
ajax跨域请求,目前已用几种方法实现: 1)用原生js的xhr对象实现. var url="http://freegeoip.net/json/" ...
- JQuery的Ajax跨域请求原理概述及实例
今天在项目中需要做远程数据加载并渲染页面,直到开发阶段才意识到ajax跨域请求的问题,隐约记得Jquery有提过一个ajax跨域请求的解决方式,于是即刻翻出Jquery的API出来研究,发 JQuer ...
- php中ajax跨域请求---小记
php中ajax跨域请求---小记 前端时间,遇到的一个问题,情况大约是这样: 原来的写法: 前端js文件中: $.ajax({ type:'get', url:'http://wan.xxx.c ...
- jquery ajax跨域请求详解
本文章来给大家详细jquery中的ajax跨域请求, 在JQuery对于Ajax的跨域请求有两类解决方案,不过都是只支持get方式.分别是JQuery的jquery.ajax jsonp格式和jque ...
随机推荐
- dedecms代码研究六
今天讲的是dedecms最关键的东西,模板分析啦.也就是dedetag.class.php 里面的ParseTemplet方法 模板解析方法 先看看一个dedecms标签,大家心里有个数: {dede ...
- regular expressions
- Eclipse/MyEclipse怎么设置个性化代码注释模板
1.打开Eclipse/MyEclipse工具,打开或创建一个Java工程,点击菜单Window->Preferences弹出首选项设置窗口 2.展开左侧Java->Code Style- ...
- bash脚本编程之一 变量、变量类型等
变量的内容 1.变量命名: 1.只能包含字母.数字和下划线,并且不能以数字开头, 2.不应该跟系统中已有的环境变量重名 3.最好能见名知意 2.变量赋值: 设置变量: ...
- 表格制作模块xlwt
import xlwtworkbook = xlwt.Workbook(encoding = 'ascii') #创建workbook 括号内容视情况而定sheetname = 'Sheet'book ...
- ( 解压缩版 免安装版 或 zip版 )如何修改mysql5.6.24 字符编码
1.当我们把zip文件格式解压到指定目录后,并且完成基本环境配置后,打开mysql 5.6.24会发现名为[my-default.ini]的文件.我们用记事本打开该文件会发现并没有[default-c ...
- php解析json数组
function urltest(){ $url='http://121.43.153.80:8983/solr/collection1/select?q=+searchkey%3a%E4%BC%9A ...
- WordPress主机
- 开源PLM软件Aras详解一 安装
对于Aras,一定很多人陌生,Aras Innovator是开源的PLM系统,优点就是免费license,没有节点限制,最棒的是基于SOAP,扩展性非常强,但是国内很多想入手的人入门很难, 关于Ara ...
- Activity生命周期(二)
------siwuxie95 在项目 ActivityLifeCircle 的 MainActivity.java 中添加方法: onCreate() onStart() onResume() ...