当我从 JQuery 调用时,我收到内部错误 500,而不是其他错误。请帮忙!!
JQuery 调用
$.ajax({
type: "POST",
url: "myWebService.asmx/getnpsTrend",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess_,
error: OnErrorCall_
});
网络服务方法:
[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public List<ChartDatasets> getnpsTrend(string region, string client, string product)
{
<code>
}
我已经尝试了除静态之外提供的所有可能的解决方案:
已启用获取
重新配置的配置文件
更多背景...
- 我正在使用图表.js来显示图形
- 为了使图表数据动态化,我正在使用网络服务为我提供 json 格式的数据。这是参考。
- 如果我直接调用网络服务,这将完美运行。所以我想知道 JQuery 调用中提供的网址是否错误!尝试了不同的组合,但还没有奏效!:(
编辑:完整的 JQuery 代码
<script>
$(document).ready(function () {
$("#btn_line_chart").on('click', function () {
var fil_reg = $("#ddlRegion").val();
var fil_cli = $("#ddlClient").val();
var fil_prd = $("#ddlProduct").val();
console.log("Inside function");
console.log(fil_reg);
var jsonData = JSON.stringify({
jsn_reg: fil_reg,
jsn_cli: fil_cli,
jsn_prd: fil_prd
});
$.ajax({
type: "POST",
url: "myWebService.asmx/getnpsTrend",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess_,
error: OnErrorCall_
});
function OnSuccess_(reponse) {
var aData = reponse.d;
var aLabels = aData[0];
var aDatasets1 = aData[1];
var aDatasets2 = aData[2];
var data = {
labels: aLabels,
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: aDatasets1
},
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: aDatasets2
}]
};
var ctx = $("#myChart").get(0).getContext('2d');
ctx.canvas.height = 300; // setting height of canvas
ctx.canvas.width = 500; // setting width of canvas
var lineChart = new Chart(ctx).Line(data, {
bezierCurve: false
});
}
function OnErrorCall_(repo) {
alert("Woops something went wrong, pls try later !");
}
});
});
</script>
在 webscript 中进行以下更改
[System.Web.Script.Services.ScriptService] // add top of Class file
public class myWebService: System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public List<ChartDatasets> getnpsTrend(string region, string client, string product)
{
//Check if we get All Parameters from Ajax function i.e region, client and product
// Code
}
}
然后在 Ajax 函数中
$.ajax({
url: '../myWebService.asmx/getnpsTrend',
data: "{'region': '" + regionvalue + "'}, 'client': '" + clientvalue + "','product': '" + productvalue + "'",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data)
{
if (data != null)
{
// use data.d value
}
},
error: function (data)
{
alert("Error message ");
}
});