为什么传递查询字符串参数时 ASP.NET webmethod 返回 500
我有以下 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;
}
}
我通过 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"
}
]
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"
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查询字符串参数需要以 JSON 形式传递。您没有正确传递它们,因此我猜测您的网络方法中的
lat
和long
为空。要正确传递它们,请使用以下命令:JSON.stringify
方法是在现代浏览器中本机实现的。如果您需要维护旧版浏览器,您可能需要包含 json2.js 脚本,该脚本将使用本机方法(如果浏览器支持),如果不支持则使用其实现。另请注意,您的 Web 方法返回一个纯字符串。这意味着在客户端
data.d
将是一个字符串。如果你想将它作为一个对象来操作,你需要首先解析它:The query string parameters need to be passed as JSON. You are not passing them correctly so I guess
lat
andlong
are null in your web method. To pass them correctly use the following:The
JSON.stringify
method is natively implemented in modern browsers. If you need to maintain legacy browsers you might need to include json2.js script which will use the native method if the browser supports it and if not use its implementation.Also note that your web method returns a plain string. This means that on the client
data.d
will be a string. If you want to manipulate it as an object you will need to parse it first:如果您收到此错误,这意味着您在概念上在某个地方做错了。理想情况下,ajax 场景仅适用于中小型服务器端调用,而不是获取大量数据,如果您使用异步请求获取大量数据,那么你是在自找麻烦。因此,在这种情况下,最好去实现服务器端逻辑。但是,如果你处于无法更改应用程序逻辑的阶段,现在将其添加到你的 web.config 中:
重要的事情请记住,整数的最大值字段为 2147483647,并且此限制必须保持在该值的范围内。
但现在我真的很担心,因为我需要这个应用程序能够扩展以供大量使用。我完成了这项工作,但请让我知道如何改进我的实践。谢谢
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:
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