从使用jQuery ASP.Net JSON服务获取数据 [英] Getting data from ASP.Net JSON service with jQuery
问题描述
我试图调用谷歌地图API地理编码,以得到一个纬度/经度对的格式的地址,然后登录到控制台。我试图获取获取给定的位置返回的第一个'的formatted_address项。
我简单的不能提取的JSON这个项目,我不知道为什么。 code的线需要提取数据,将大大AP preciated。
JavaScript的:
//获取用户的当前位置
功能的getLocation()
{
如果(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}}功能showPosition(位置)
{
VAR纬度= position.coords.latitude;
VAR经度= position.coords.longitude; $阿贾克斯({
键入:POST,
网址:ReportIncident.aspx / ReverseGeo code,
数据:{纬度:+纬度+,经度:+经度+},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:函数(returnedData)
{
//执行console.log(/ ****的formatted_address这里请**** /);
}
});
}
C#的:
[的WebMethod]
公共静态字符串ReverseGeo code(数字纬度,经度十进制)
{
//创建web请求 字符串的URL =http://maps.googleapis.com/maps/api/geo$c$c/json?latlng=+纬度+,+经度+
&放大器;传感器=真正的;
HttpWebRequest的要求= WebRequest.Create(URL)为HttpWebRequest的; //获取响应
使用(HttpWebResponse响应= request.GetResponse()作为HttpWebResponse)
{
//获取响应流
StreamReader的读者=新的StreamReader(response.GetResponseStream()); //控制台应用程序输出
返回reader.ReadToEnd();
}
}
您JSON被转义,因为你回来从你的的WebMethod
,而不是让一个字符串内置在的JavaScriptSerializer
做的事情。
大概,你看到的是:
\\ {/ *数据* / \\}
而不是这样的:
{/ *数据* /}
您可以通过评估运行返回的字符串解决这个问题()
但是请注意,我不建议...只是说的可以
修改
$阿贾克斯({
键入:POST,
网址:ReportIncident.aspx / ReverseGeo code,
数据:{纬度:+纬度+,经度:+经度+},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:函数(returnedData)
{
VAR realReturnedData =的eval(returnedData.d);
// realReturnedData现在有你期待的谷歌地图的数据结构
}
});
再... 的eval是邪恶的的(如果你不是100%信任的来源)。我会建议返回从您的WebMethod ...
不同的数据类型编辑2
[的WebMethod]
公共静态MyClass的ReverseGeo code(十进制经纬度,十进制长){
MyClass的OBJ =新MyClass的();
//做东西
返回OBJ;
}
但我真正的问题是为什么你跟一个Web服务调用到服务器这样做呢?您可以直接做地理编码从Javascript谷歌的API <一个href=\"https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-reverse\" rel=\"nofollow\">https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-reverse
I am trying to call the Google Maps geocoding API, to get a formatted address from a lat/long pair, and then log it to the console. I am trying to get the first 'formatted_address' item that gets returned for a given location. I am simple unable to extract that item from the JSON, I have no idea why. The line of code needed to extract the data would be greatly appreciated.
The javascript:
//Gets the current location of the user
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.ajax({
type: "POST",
url: "ReportIncident.aspx/ReverseGeocode",
data: "{latitude:" + latitude + ",longitude:" + longitude + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (returnedData)
{
// console.log(/****formatted_address Here Please****/);
}
});
}
The C#:
[WebMethod]
public static string ReverseGeocode(decimal latitude, decimal longitude)
{
// Create the web request
string url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude +
"&sensor=true";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
return reader.ReadToEnd();
}
}
Your JSON is escaped because you're returning a string from your WebMethod
, and not letting the built in JavascriptSerializer
do it's thing.
roughly, you're seeing this:
"\{ /*data*/ \}"
instead of this:
{ /*data*/ }
You can fix this by running the returned string through eval()
but note that I'm not recommending it... just saying you can
EDIT:
$.ajax({
type: "POST",
url: "ReportIncident.aspx/ReverseGeocode",
data: "{latitude:" + latitude + ",longitude:" + longitude + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (returnedData)
{
var realReturnedData = eval(returnedData.d);
//realReturnedData now has the google maps data structure you're expecting
}
});
Again... eval is evil (if you don't 100% trust the source). I would recommend returning a different data type from your webmethod...
EDIT 2:
[WebMethod]
public static MyClass ReverseGeocode(decimal lat, decimal long) {
MyClass obj = new MyClass();
//do stuff
return obj;
}
But my real question is why are you doing this with a webservice call to your server? You can directly do geocoding from Javascript to google's API https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-reverse
这篇关于从使用jQuery ASP.Net JSON服务获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!