【发布时间】:2014-02-25 10:06:43
【问题描述】:
我有一个 Web 服务 WCF,我想从 Jquery 中检索数据。
但成功在 msg 中返回 null :
$.ajax({
type: 'GET',
url: 'http://localhost:52768/Service1/Statistic_1',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
response = msg.Items;
console.log(msg);
for (var i = 0; i < response.length; i++) {
allstat1[i] = [response[i].Geografisch_zone];
}
fillDataTable(allstat1);
},
error: function (e) {
alert("error loading statistic 1");
}
});
}
我使用了调试,它在 SqlConnection 的 Statistic_1 方法中捕获了一个异常:
public static List<Statistic_1> Helper_Statistic_1()
{
List<Statistic_1> result = new List<Statistic_1>();
Statistic_1 stat1 = null;
int yearstart = DateTime.Today.AddYears(-4).Year;
int yearend = DateTime.Today.Year;//2014
string lastGeographischZone = null;
string query = "SELECT Year ...";
using (SqlConnection connection = new SqlConnection(Context.db.Connection.ConnectionString))
{
...
}
这里是 web.config:
<configuration>
<connectionStrings>
<add name="RMS_DatabaseConnectionString" connectionString="Data Source=****;Initial Catalog=RMS_Database;Persist Security Info=True;User ID=****;Password=****"providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="16384"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="basicHttp" allowCookies="true" maxReceivedMessageSize="20000000" maxBufferSize="20000000" maxBufferPoolSize="20000000">
<readerQuotas maxDepth="32" maxArrayLength="200000000" maxStringContentLength="200000000"/>
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
【问题讨论】:
-
您能更具体地谈谈您的问题吗?问题在哪里?什么不是必需的?
-
我想在 $.ajax 中检索我的 Statistic_1 方法的数据。但是成功返回 null 值,我在 SqlConnection 的 Statistic_1 方法中遇到异常...为什么?
-
“对象引用未设置为对象的实例。”当某个对象为空并且您尝试访问该对象的属性时发生,因此首先从发生此错误的位置确认。
-
我不明白为什么我在 SqlConnection 处出现异常。
-
使用此 ConfigurationManager.AppSettings["RMS_DatabaseConnectionString"].ToString()