2011-03-13 21 views
1

スクリプト私は初心者ですjquery.ajax

$.ajax({ 
    type: "post", 
    url: "Default.aspx?cmd=Setting", 
    success: parseXml 
    }); 

function parseXml(xml) 
{ 
    alert(xml);//show Full XML File 
    //find every Tutorial and print the author 
    $(xml).find("Tutorial").each(function() 
    { 
    $("#a").append($(this).attr("author") + "<br />"); 
    }); 
} 

HTML

<div id="a"></div> 

コード

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Request["cmd"] == "Setting") 
    { 
     string [email protected]"<?xml version='1.0' encoding='utf-8' ?> 
     <RecentTutorials> 
     <Tutorial author='The Reddest'> 
     <Title>Silverlight and the Netflix API</Title> 
     <Categories> 
       <Category>Tutorials</Category> 
       <Category>Silverlight 2.0</Category> 
       <Category>Silverlight</Category> 
       <Category>C#</Category> 
       <Category>XAML</Category> 
     </Categories> 
     <Date>1/13/2009</Date> 
     </Tutorial> 
     </RecentTutorials>"; 

      Response.Write(k); 
      Response.End(); 
    } 
} 

でXMLデータを取得します。

これは機能しません。

alert(xml)はxmlファイルを表示します。

+0

「動作しません」と定義してください。 –

+0

'parseXml'の中で' alert(xml); 'を試したことがあります –

+0

xmlを表示してdivに追加しません – beginner

答えて

0

XMLにデータ型を強制的に試してみてください。

Response.ContentType = "text/xml"; 
Response.Write(k); 
Response.End(); 

また、あなたがdataType: 'xml'を設定することができますが、それは必要はありません:jQueryのは、自動的にXMLをパース有するために、サーバー上の適切なコンテンツタイプを設定しdataType: 'xml'

+1

結果的には' Response.ContentType = "text/xml; charset = utf-8"; 'サーバーコードで – Oleg

1

サーバーが適切なコンテンツタイプを送信するように正しく設定されているかどうか。

ここにはlive demoがあります。