在XMLHttpRequest.open()中传递URL

时间:2014-07-10 17:03:09

标签: html xmlhttprequest

当我传递硬编码的URL时,它的工作正常,但是当使用其参数创建url时,它无法正常工作

var server = "localhost";
  var port = "8080";
  var db = "US";
  var lon = "-77.092609";
  var lat = "38.871256";
  var epsg = "epsg:4326";
  var inp= "5";

  var url = '"http://'+server+':'+port+'/rest/'+db+'.json?q=travel&point='+lon+','+lat+','+epsg+'&inp='+inp+'"'; 
  document.write(url);
xmlhttp.open("GET","http://localhost:8080/rest/US.json?q=travel&point=-77.092609,38.871256,epsg:4326&inp=5",true);//WORKS FINE
xmlhttp.open("GET",url,true);//DOES NOT WORK!!

我做错了什么?

1 个答案:

答案 0 :(得分:0)

在字符串的开头和结尾取出双引号。

var url = 'http://'+server+':'+port+'/rest/'+db+'.json?q=travel&point='+lon+','+lat+','+epsg+'&inp='+inp;