PDA

View Full Version : سوال: اجرا نشدن کد



woeful
دوشنبه 23 مرداد 1391, 10:55 صبح
سلام

من یه کد رو از یک کتاب آمورشی نوشتم اجرا نمیشه ۱ روزه روش گیر کردم نمیدونم مشکلش کجاست لطفا راهنمایی کنید .


<html>
<head>
<title>This is test3</title>
<script type="text/javascript">
var xmlHttp;
var requestType="";
function activeObject()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}

function startRequest(list)
{
requestType = list;
activeObject();
xmlHttp.onreadystatechange = handle;
xmlHttp.open("GET" , "states.xml" , true);
xmlHttp.send(null);
}


function handle()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
if(requestType == "north")
{
showNorth( );
}
else if(requestType == "all")
{
showAll();
}
}
}
}

function showNorth()
{
var xmdDoc = xmlHttp.responseXML;
var northNode = xmlDoc.getElementsByTagName("north")[0];
alert("yooooooooo");
var out = "Northern States";
var northStates = northNode.getElementsByTagName("state");
outputList("Northen States",northStates);
}

function showAll()
{
var xmlDoc = cmlHttp.responseXML;
var allStates = xmlDoc.getElementsByTagName("state");
outputList("All states in Document",allStates);
}

function outputList(title , array)
{
var out = title;
var current = null;
for(var i=0;i<array.length;i++)
{
current = array[i];
out = out + "\n- " + current.childNodes[0].nodeValue;
}
alert(out);
}

</script>
</head>
<body>
<h1>Some State</h1>

<form action="#">
<input type="button" value="Click To Get Northen State" onClick="startRequest('north');"/>

<input type="button" value="Click To Get Northen State" onclick="startRequest('all');"/>
</form>
</body>
</html>



اینم فایل XML که در کد از سرور درخواست میشه

<?xml version="1.0" encoding="UTF-8"?>
<states>
<north>
<state>Minnesota</state>
<state>Iowa</state>
<state>North Dakota</state>
</north>
<south>
<state>Texas</state>
<state>Oklahoma</state>
<state>Louisiana</state>
</south>
<east>
<state>New York</state>
<state>North Carolina</state>
<state>Massachusetts</state>
</east>
<west>
<state>California</state>
<state>Oregon</state>
<state>Nevada</state>
</west>
</states>

ممنون میشم کمک کنید مشکل این کد کجاست

hossin.esm
دوشنبه 23 مرداد 1391, 15:58 عصر
سلام
یکی مشکل خط 49-50 که اسم متغیر ها با هم یکی نیست

var xmdDoc = xmlHttp.responseXML; var northNode = xmlDoc.getElementsByTagName("north")[0];


صحیحش


var xmlDoc = xmlHttp.responseXML;
var northNode = xmlDoc.getElementsByTagName("north")[0];


یکی دیگه خط 59

var xmlDoc = cmlHttp.responseXML;


صحیحش

var xmlDoc = xmlHttp.responseXML;

در ضمن علامت سوال خط 40 را هم پاک کن

ice_girl68
پنج شنبه 26 مرداد 1391, 16:52 عصر
سلام به همگی دوستان.منم یه کد از یه pdf کپی کردم که کار نمیکنه میشه ایرادشو بهم بگید؟
فایل country.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
$states=array();
$states=["kordestan"]=array("new South Wales","Queen","victoria");
$states["Australia"]=array("sanandaj","bane","marivan");
if(isset($_POST["country"]) && isset($states[$_POST["country"]]))
{
foreach($states[$_POST["country"]] as $state){
printf("%s,"$state);
}
}



?>

</body>
</html>


فایل country.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var url="country.php";
var what="setstates(req.responseText)";
function GetStates(country){
DoCallback("country=" + country);
}
function SetStates(States){
var stateBox= document.getElementById("state");
stateBox.option.length=0;
if(states!=""){
var arrStates=States.split(",");
for(i-0;i<arrStates.length;i++){
if(arrStates[i]!=""){
stateBox.option[stateBox.option.length]=new Option(arrStates[i],arrStates[i]);
}
}
}
}
</script>
<script src="ajax.js" type="text/javascript"></script>


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<pre> country:
<select onchange="GetStates(this.options[this.selectedIndex].text)" id="country">
<option value=""></option>
<option>kordestan</option>
<option>Australia</option>
</select><br />state: <select id="state"></select></pre>

</body>
</html>


فایل ajax.js


// JavaScript Document
function DoCallback(data){
if(window.XMLHttpRequest(){
req=new XMLHttpRequest();
req.onreadystatechange=processReqChange;
req.open('POST',url,true);
req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
req.send(data);
}

else if(window.ActiveXObject){
req=new ActiveXObject('Microsoft.XMLHTTP');
if(req){
req.onreadystatechange=processReqChange;
req.open('POST',url,true);
req.SetRequestHeader('Content-Type','application/x-www-form-urlencoded');
req.send(data);
}
}
}

function ProcessReqChange(){
if(req.readyState==4){
if(req.status==200){
eval(what);
}
else{
alert('there was a problem:' + req.responseText);
}
}
}

popcorn
پنج شنبه 26 مرداد 1391, 22:54 عصر
سلام به همگی دوستان.منم یه کد از یه pdf کپی کردم که کار نمیکنه میشه ایرادشو بهم بگید؟



}

پستت رو ویرایش کن و کدهات روی تگ کد (مثل پست اول) قرار بده تا بهتر بتونبم کد رو ببینیم و کجای کد شما ایراد داره یعنی چه جایی گیر می کنه ؟