国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JQuery Ajax通過Handler訪問外部XML數據的代碼

2024-05-06 12:37:04
字體:
來源:轉載
供稿:網友
JQuery的使用非常簡單,我們只需要從其官方網站上下載一個腳本文件并引用到頁面上即可,然后你就可以在你的腳本代碼中任意使用JQuery提供的對象和功能了。
  在JQuery中使用Ajax方法異步獲取服務器資源非常簡單,讀者可以參考其官方網站上提供的例子http://api.jquery.com/category/ajax/。當然,作為客戶端腳本,JQuery也會遇到跨域訪問資源的問題,什么是跨域訪問呢?簡單來說就是腳本所要訪問的資源屬于網站外部的資源,腳本所在的位置和資源所在的位置不在同一區域。默認情況下,瀏覽器是不允許直接進行資源的跨域訪問的,除非客戶端瀏覽器有設置,否則訪問會失敗。在這種情況下,我們一般都會采用在服務器端使用handler來解決,就是說在腳本和資源之間建立一個橋梁,讓腳本訪問本站點內的handler,通過handler去訪問外部資源。這個是非常普遍的做法,而且操作起來也非常簡單,因為會經常使用到,所以在此記錄一下,方便日后使用!
  首先需要在網站中創建一個handler,在Visual Studio中新建一個Generic Handler文件,拷貝下面的代碼:
代碼如下:
<%@ WebHandler Language="C#" Class="WebApplication1.Stock" %>
namespace WebApplication1
{
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Asynchronous HTTP handler for rendering external xml source.
/// </summary>
public class Stock : System.Web.IHttpAsyncHandler
{
private static readonly SafeList safeList = new SafeList();
private HttpContext context;
private WebRequest request;
/// <summary>
/// Gets a value indicating whether the HTTP handler is reusable.
/// </summary>
public bool IsReusable
{
get { return false; }
}
/// <summary>
/// Verify that the external RSS feed is hosted by a server on the safe list
/// before making an asynchronous HTTP request for it.
/// </summary>
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
var u = context.Request.QueryString["u"];
var uri = new Uri(u);
if (safeList.IsSafe(uri.DnsSafeHost))
{
this.context = context;
this.request = HttpWebRequest.Create(uri);
return this.request.BeginGetResponse(cb, extraData);
}
else
{
throw new HttpException(204, "No content");
}
}
/// <summary>
/// Render the response from the asynchronous HTTP request for the RSS feed
/// using the response's Expires and Last-Modified headers when caching.
/// </summary>
public void EndProcessRequest(IAsyncResult result)
{
string expiresHeader;
string lastModifiedHeader;
string rss;
using (var response = this.request.EndGetResponse(result))
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 天津市| 德安县| 巴彦淖尔市| 高州市| 湖南省| 红桥区| 桃江县| 桐乡市| 华容县| 江城| 河西区| 渭源县| 天长市| 三穗县| 临泉县| 伊春市| 友谊县| 永康市| 黑河市| 南康市| 宝清县| 临夏市| 日喀则市| 长岭县| 博兴县| 巩留县| 张北县| 定安县| 浦城县| 郎溪县| 高碑店市| 稷山县| 海南省| 宽甸| 汶上县| 巴林右旗| 鄂伦春自治旗| 犍为县| 白玉县| 新巴尔虎左旗| 灌南县|