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 ...
随机推荐
- JS和CSS关于大小写的区分
方法: document.getElementById("xx").style.xxx中的所有属性是什么 盒子标签和属性对照 CSS语法(不区分大小写) JavaScript语法( ...
- PreparedStatement解决sql注入问题
总结 PreparedStatement解决sql注入问题 :sql中使用?做占位符 2.得到PreparedStatement对象 PreparedStatement pst=conn.prepar ...
- go特性学习
而后我写了一些测试代码: dept1 := Dept{ name: "MySohu", building: "Intern ...
- 0601 Spring2.0 发布会及产品发展方向
ProductBacklog:继续向下细化; 1.界面美化,统一界面风格,以简洁美观为主: 2.丰富版面的内容,吸引用户: 3.尝试增加新的版面: Sprint 计划会议:确定此次冲刺要完成的目标 1 ...
- leetcode 168
168. Excel Sheet Column Title Given a positive integer, return its corresponding column title as app ...
- Linux网络常用指令
5.1 网络参数设定使用的指令 ifconfig 查询 设定网络卡与 IP 网域等相关参数: ifup, ifdown 这两个档案是 script,透过更简单的方式来启动网络接口: route 查 ...
- Web前端之CSS_day3-4
1.行高 1.1 初始行高 行高=文字大小+上间距+下间距 默认文字大小:16px 默认文字行高:18px 注意:行高=盒子的高度,可以让文字垂直居中显示 1.2 行高单位 a. px 行高设置多少就 ...
- spi 子系统
http://blog.csdn.net/ropenyuan/article/details/42269641 http://blog.chinaunix.net/uid-27406766-id-33 ...
- NLPP-02-Exercises
<PYTHON自然语言处理>第2章 内容简介:各种文本语料库. 古腾堡项目 www.gutenberg.org OLAC元数据格式档 http://www.language-archive ...
- 使用引脚模拟PWM波控制引脚
/********************************* 代码功能:输出PWM波控制引脚 使用函数: 创作时间:2016*10*07 作者邮箱:jikexianfeng@outlook.c ...