JQuery跨域解決方案
来源:互联网 发布:婴儿床上装饰 知乎 编辑:程序博客网 时间:2024/05/22 07:48
1.相關JS代碼
<script type="text/javascript">
$(document).ready(function(){
//begin 縣市
$("#iSearch_countyId1").change(function(){
$.getJSON("http://www.cthouse.com.tw/New/GetTownshipSelectOptionJson.ashx?ct_parent=" + $("#iSearch_countyId1").val() + "&jsoncallback=?", function(data){
//var result = eval("(" + data + ")");
$.each(data, function(i,n){
//alert(n);
$("#iSearch_townshipId1").empty();
$("#iSearch_townshipId1").append(n);
});
});
});
});
</script>
2.相關HTML代碼
<select id="iSearch_countyId1" name="countyId1" >
<option>全部</option>
</select>
3.後台ashx代碼
<%@ WebHandler Language="C#" Class="GetTownshipSelectOptionJson" %>
using System;
using System.Web;
public class GetTownshipSelectOptionJson : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string ct_parent = context.Request.QueryString["ct_parent"];
string default_val = context.Request.QueryString["default_val"];
string jsoncallback = context.Request.QueryString["jsoncallback"];
string options = CascadeUtility.GetCodeCityTownOptions(ct_parent, "");
string json = jsoncallback + "(";
json += "{\"options\":\"" + options.Replace("\"", "\\\"").Replace("\n", "\\n") + "\"}";
json += ")";
//json = jsoncallback + "({\"options\":\"aaa\\\"aaa\"})";
context.Response.Write(json);
}
public bool IsReusable {
get {
return false;
}
}
}
說明
$.getJSON()方法裡,跨域一定要注意 jsoncallback=? 參數,它會自動生成一個字符串,後台返回的JSON字符串必須以它命名。
$(document).ready(function(){
//begin 縣市
$("#iSearch_countyId1").change(function(){
$.getJSON("http://www.cthouse.com.tw/New/GetTownshipSelectOptionJson.ashx?ct_parent=" + $("#iSearch_countyId1").val() + "&jsoncallback=?", function(data){
//var result = eval("(" + data + ")");
$.each(data, function(i,n){
//alert(n);
$("#iSearch_townshipId1").empty();
$("#iSearch_townshipId1").append(n);
});
});
});
});
</script>
2.相關HTML代碼
<select id="iSearch_countyId1" name="countyId1" >
<%= iSearchUtil.getCityTownList("0", "", 1, 1, iSearch)%>
</select>
<select id="iSearch_townshipId1" name="townshipId1"></select>
<option>全部</option>
</select>
3.後台ashx代碼
<%@ WebHandler Language="C#" Class="GetTownshipSelectOptionJson" %>
using System;
using System.Web;
public class GetTownshipSelectOptionJson : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string ct_parent = context.Request.QueryString["ct_parent"];
string default_val = context.Request.QueryString["default_val"];
string jsoncallback = context.Request.QueryString["jsoncallback"];
string options = CascadeUtility.GetCodeCityTownOptions(ct_parent, "");
string json = jsoncallback + "(";
json += "{\"options\":\"" + options.Replace("\"", "\\\"").Replace("\n", "\\n") + "\"}";
json += ")";
//json = jsoncallback + "({\"options\":\"aaa\\\"aaa\"})";
context.Response.Write(json);
}
public bool IsReusable {
get {
return false;
}
}
}
說明
$.getJSON()方法裡,跨域一定要注意 jsoncallback=? 參數,它會自動生成一個字符串,後台返回的JSON字符串必須以它命名。
0 0
- JQuery跨域解決方案
- jQuery插件管理方案
- Jquery优化方案
- JQuery+Bootstrap一揽子方案
- JQuery+Bootstrap一揽子方案
- Jquery parseInt函数问题解决方案
- jquery 跨域访问方案
- jquery demo 部分问题解决方案总结
- JQuery向ashx提交中文参数方案
- 使用jQuery时的性能优化方案
- jquery 跨域POST信息问题解决方案
- 使用jQuery时的性能优化方案
- ecshop与jquery的冲突问题解决方案
- jquery.getJSON跨域方案实现原理
- 移动端jquery替换方案zepto.js
- jQuery-jquery+ajax+jsonp+java跨域请求方案
- Visual Studio 2010 Web项目的jQuery版本升级方案
- Visual Studio 2010 Web项目的jQuery版本升级方案
- grails项目输出日志设定
- SWFObject: 基于Javascript的Flash媒体版本检测与嵌入模块
- JQuery中$.ajax()方法参数详解
- JavaScript定义类的几种方式
- SQL Server 2005 sa 用户被锁定的问题
- JQuery跨域解決方案
- 遍历Hashtable、IDictionary、Dictionary<string, string>
- javascript复制到粘贴板
- 形象工程:创建型设计模式总结
- 项目代码:基金行事历JQuery代码
- 项目代码-存储过程(复制表结构和数据、日期函数、随机数函数)
- HTC修复音乐乱码的两种方法
- SQL查询所有新闻分类且关联出每个分类下的最新的一条新闻
- JQuery动态创建表单:ie下select无法下拉(firefox正常)