最近使用了百度的API功能,但调用方法是这样的http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=116.254615&y=29.814476
返回格式:
{"error":0,"x":"MTE2LjI2MTA5OTEyMjE=","y":"MjkuODIwNTYwODc0ODQ2"} 请问各位兄弟,这种方法,在JAVASCRIPT里面怎么调用呀。
返回格式:
{"error":0,"x":"MTE2LjI2MTA5OTEyMjE=","y":"MjkuODIwNTYwODc0ODQ2"} 请问各位兄弟,这种方法,在JAVASCRIPT里面怎么调用呀。
var obj = eval('('+ str +')');
alert(obj.error)
alert(obj.x)
alert(obj.y)
WebResponse wp = wr.GetResponse();
Stream s = wp.GetResponseStream();
StreamReader sr = new StreamReader(s);
string str = sr.ReadToEnd();
Response.Write(str);
sr.Dispose();
s.Dispose();
要使用 System.Net 和 System.IO命名空间
Server Error in '/WebMapinfo' Application.
--------------------------------------------------------------------------------The remote server returned an error: (407) Proxy Authentication Required.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.Source Error:
Line 20:
Line 21: WebRequest wr = WebRequest.Create("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=116.254615&y=29.814476");
Line 22: WebResponse wp = wr.GetResponse();
Line 23: Stream s = wp.GetResponseStream();
Line 24: StreamReader sr = new StreamReader(s);
返回了这个错误
WebProxy proxy = new WebProxy("proxy.cccil.com",8000);
proxy.Credentials = new NetworkCredential("km35609", "qwe1234!", "proxy.cccil.com");
//访问baidu方法
WebRequest wr = WebRequest.Create("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=116.254615&y=29.814476");
wr.Proxy = proxy;
WebResponse wp = wr.GetResponse();
Stream s = wp.GetResponseStream();
StreamReader sr = new StreamReader(s);
string str = sr.ReadToEnd();
Response.Write(str);
sr.Dispose();
s.Dispose();已解决,请各位高手提供下JAVASCRIPT的方法
$.getJSON("http://api.map.baidu.com/ag/coord/convert?callback=?", {
from: 2,
to: 4,
x: 116.254615,
y: 29.814476
}, function (data) {
alert(data.x);
});
});因为已经涉及到跨域访问,如果用纯javascript解决的话会比较麻烦,但是用jquery的getJSON就比较方便了
$.getJSON("http://api.map.baidu.com/ag/coord/convert?callback=?",
{//这里是你那个url的参数
from: 2,
to: 4,
x: 116.254615,
y: 29.814476
},
function (data) {
//这里就是那个baidu页面返回的数据 {"error":0,"x":"MTE2LjI2MTA5OTEyMjE=","y":"MjkuODIwNTYwODc0ODQ2"}
//比如要获得 error的代码,就用 data.error 来获取, x就是data.x,y就是data.y,这就是json格式
alert(data.x);
});
});