У меня есть следующий веб-метод:
[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"
}
]