I have the following webmethod:
[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;
}
}
which I am calling via 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;
}
});
the problem is the current LocalCrime string breaks with a 500 but if I replace it with:
String LocalCrime = "http://policeapi2.rkh.co.uk/api/leicestershire/C01/crime"
Then suddenly without the query string values it works.
Any help is much appreciated.
Okay I wrote a proxy class so I can debug now. return results;
does bring back what I want but it is inside an array, maybe that has to do with it. my data looks like this (probably something in the client side is getting this data wrong):
[
{
"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"
}
]
If you are getting this error, this means you are conceptually doing wrong somewhere.Ideally ajax scenarios are meant only for small to medium server side calls not for getting tons of data, and if you are getting a lot of data using the asynchronous request then you are asking for trouble.So in such scenarios it is best to go and implement server side logic.But what if you are at a stage where you can't change your application logic now add this to your web.config:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"></jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
Important thing to keep in mind is that the maximum value for the integer field is 2147483647 and this limit has to remain inside the bounds of this value.
But now I am really worried as I need this app to be scalable for heavy use. I made this work but please let me know how can I improve my practice. Thank you