开发者

javascript ajax: get value

i have problem in getting and testing the return value of ajax. my code is, and return when alert is => Nan.

function getValue(table1, table2, id1, id2)
{
 alert("table name "+table2+" id: "+id2);

 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }

 xmlhttp.onreadystatechange=function()
 {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("ajax value: "+xmlhttp.responseText - 0);

var val =   xmlhttp.responseText - 0;

var price 开发者_开发知识库= document.getElementById("price");
var original= price.value;
original    =   original - 0;
var new_val =   original + val;
price.value = new_val;

document.getElementById("showprice").innerHTML=xmlhttp.responseText;
 }
}

xmlhttp.open("GET","_pages/ajax/advert_temp.php?    t1="+table1+"&t2="+table2+"&id1="+id1+"&id2="+id2+"",true);
xmlhttp.send();

}


I am assuming you are talking about this alert.

alert("ajax value: "+xmlhttp.responseText - 0);

Order of operations here says

  1. Add "ajax value: " and xmlhttp.responseText
  2. Take result of 1 and subtract zero [aka "stringNumber" - 0 = NaN]

You need to change the order of operations so it is

alert("ajax value: " + (xmlhttp.responseText - 0) );

But it really makes no difference in your alert if it is a string or a number since the alert displays strings.

You can also use parseInt or parseFloat for converting numbers which some people might like to read instead of -0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜