[英]JSON return data is always null from WebService (C#.NET)
I have stepped through my WebServices code to validate that I am actually returning data, but when I check the return on the JSON side, it always says "null." 我已经逐步完成了WebServices代码,以验证我实际上是在返回数据,但是当我检查JSON端的返回值时,它始终显示为“ null”。
Here is a snippet of the WebService code. 这是WebService代码的片段。
UPDATED : To simplify things greatly, I'm returning a simple List from the web service instead of a custom object, and I've condensed the AJAX call as well. 更新 :为简化起见,我从Web服务返回了一个简单的List而不是一个自定义对象,并且我也压缩了AJAX调用。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<string> GetLocations(string CustomerID)
{
List<string> LocationsList = new List<string>();
string sql = "select customer_site from demand_addresses where customer_identifier=" + CustomerID;
if (gDB.ExecuteRet(sql, "DB"))
{
DataTable dt = gDB.GetResultDataSet(0);
int i = 0;
if (dt.Rows.Count > 0)
{
foreach (DataRow rs in dt.Rows)
{
LocationsList.Add(rs["customer_site"].ToString());
}
}
else
{
LocationsList.Add("No Data Found.");
}
return LocationsList;
}
else
{
LocationsList.Add("No Data Found.");
return LocationsList;
}
And here is the AJAX call ( UPDATED to reflect Kami's comments below): 这是AJAX调用(已更新,以反映以下Kami的评论):
$.ajax({
type: "POST",
url: "/webservices/services.asmx/GetLocations",
data: "{ 'CustomerID': '" + selectedCustomer + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) { alert(data.d); },
error: function (status) {alert(status);}
});
The "alert(data.d)" line results in "TypeError: data is null", and if I change it to "alert(data)" I simply get an alert box that says "null." “ alert(data.d)”行导致“ TypeError:数据为空”,如果将其更改为“ alert(data)”,我只会得到一个警告框,显示“空”。
I need help understanding why the web service is properly returning data, but it's not making it back to AJAX/JSON. 我需要帮助来理解为什么Web服务会正确返回数据,但并没有使它返回AJAX / JSON。
You are not passing the variable to the OnSuccess
function. 您没有将变量传递给OnSuccess
函数。
Also, I have had issues in the past where you need to use an anonymous function to call your subfunction. 另外,过去我遇到过一些问题,您需要使用匿名函数来调用子函数。
Try something like this 试试这个
$.ajax({
type: "POST",
url: "/webservices/services.asmx/GetLocations",
data: "{ 'CustomerID': '" + selectedCustomer + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data, resp) { OnSuccess(data,resp)}, // anon function may be needed
error: function(data,resp) { OnError(data,resp)}
});
I was working on wcf webservice a days back and same was happening to me. 几天前,我正在开发wcf webservice,同样的事情也在发生。 At the final return statement everything was working fine. 在最后的归还声明中,一切正常。 The data was there but when i saw it on the other side there was nothing. 数据在那里,但是当我在另一侧看到它时,什么也没有。
The problem was custom object (in your case LOCATION) had a nullable property but was not defined as nullable. 问题是自定义对象(在您的情况下为LOCATION)具有可为空的属性,但未定义为可为空。 I did that and it started working. 我做到了,它开始起作用。 I am still not sure why but you should try it once. 我仍然不确定为什么,但是您应该尝试一次。
public class Location
{
public string? customer_identifier { get; set; }
public string[]? customer_site { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.