【发布时间】:2011-06-19 02:39:14
【问题描述】:
我有以下网络方法:
[WebMethod(true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static String requestLocalCrime(string lat, string lng)
{
try
{
// Initialize the WebRequest.
String LocalCrime = "http://policeapi2.rkh.co.uk/api/crimes-street/all-crime?lat=" + lat + "&lng=" + lng + "";
WebRequest webRequest;
WebResponse webResponse;
webRequest = HttpWebRequest.Create(LocalCrime) as HttpWebRequest;
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.ContentType = "application/json; charset=utf-8";
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
webRequest.Credentials = new NetworkCredential(
ConfigurationManager.AppSettings["PoliceAPIUsername"].ToString(),
ConfigurationManager.AppSettings["PoliceAPIPassword"].ToString());
// Return the response.
webResponse = webRequest.GetResponse();
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), encode))
{
string results = reader.ReadToEnd();
reader.Close();
webResponse.Close();
return results;
}
}
catch(Exception e) {
return e.Message;
}
}
我通过 jQuery 调用:
var params = {
"lat": 50.819522,
"lng": -0.13642
}
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "lat": params.lat, "lng": params.lng }),
url: "crimerequest.aspx/requestLocalCrime",
dataType: "json",
// success: insertCallback
success: function (data) {
var result = $.parseJSON(data.d);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status); // Returns 500
alert(thrownError); // Internal Server Error
return false;
}
});
问题是当前 LocalCrime 字符串以 500 中断,但如果我将其替换为:
String LocalCrime = "http://policeapi2.rkh.co.uk/api/leicestershire/C01/crime"
然后突然没有它的查询字符串值。
非常感谢任何帮助。
好的,我写了一个代理类,所以我现在可以调试了。 return results; 确实带回了我想要的东西,但它在一个数组中,也许这与它有关。我的数据看起来像这样(可能是客户端的某些东西弄错了这些数据):
[
{
"category": "other-crime",
"id": 378815,
"location": {
"latitude": "50.8188090",
"street": {
"id": 379,
"name": "On or near Abbey Road"
},
"longitude": "-0.1196796"
},
"context": "",
"month": "2011-04"
},
{
"category": "anti-social-behaviour",
"id": 377906,
"location": {
"latitude": "50.8279907",
"street": {
"id": 4721,
"name": "On or near Albion Street"
},
"longitude": "-0.1336384"
},
"context": "",
"month": "2011-04"
},
{
"category": "anti-social-behaviour",
"id": 377849,
"location": {
"latitude": "50.8279907",
"street": {
"id": 4721,
"name": "On or near Albion Street"
},
"longitude": "-0.1336384"
},
"context": "",
"month": "2011-04"
},
{
"category": "other-crime",
"id": 377801,
"location": {
"latitude": "50.8279907",
"street": {
"id": 4721,
"name": "On or near Albion Street"
},
"longitude": "-0.1336384"
},
"context": "",
"month": "2011-04"
},
{
"category": "burglary",
"id": 377781,
"location": {
"latitude": "50.8279907",
"street": {
"id": 4721,
"name": "On or near Albion Street"
},
"longitude": "-0.1336384"
},
"context": "",
"month": "2011-04"
},
{
"category": "vehicle-crime",
"id": 376569,
"location": {
"latitude": "50.8279446",
"street": {
"id": 6312,
"name": "On or near Alexandra Villas"
},
"longitude": "-0.1454119"
},
"context": "",
"month": "2011-04"
},
{
"category": "anti-social-behaviour",
"id": 376525,
"location": {
"latitude": "50.8279446",
"street": {
"id": 6312,
"name": "On or near Alexandra Villas"
},
"longitude": "-0.1454119"
},
"context": "",
"month": "2011-04"
},
{
"category": "anti-social-behaviour",
"id": 376519,
"location": {
"latitude": "50.8279446",
"street": {
"id": 6312,
"name": "On or near Alexandra Villas"
},
"longitude": "-0.1454119"
},
"context": "",
"month": "2011-04"
},
{
"category": "anti-social-behaviour",
"id": 376518,
"location": {
"latitude": "50.8279446",
"street": {
"id": 6312,
"name": "On or near Alexandra Villas"
},
"longitude": "-0.1454119"
},
"context": "",
"month": "2011-04"
}
]
【问题讨论】:
-
您是否在服务器上遇到任何异常,根据您的服务器代码,您应该会看到一些异常。
-
问题是我在代理后面,无法从本地主机连接到远程主机。我每次都必须上传到服务器来尝试它,我得到的只是 500 内部错误
-
@J.W.:这通常对你来说是正确的吗?我的意思是这是为这个 uri 设置参数的正确方法吗?
-
您可以在粘贴的代码中访问 e.Message catch(Exception e) { return e.Message; }?
-
@JW:我的代码没有遇到异常,它返回结果
标签: jquery asp.net json httpwebrequest webmethod