“使用JQuery / AJAX将JSON发送到Web服务时,”对象引用未设置为对象的实例“

时间:2014-05-29 19:44:17

标签: c# javascript jquery ajax json

编辑:在弄清楚如何单步执行代码后,显示An exception of type 'System.NullReferenceException' occurred in DataManagement.dll but was not handled in user code。它发生在这一行:

string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");

我不确定这意味着什么,但希望能更好地解决这个问题。

我正在尝试将一个JavaScript对象发送到Web服务,并且我收到500错误:"对象引用未设置为对象的实例"。

我非常肯定我将某些格式化的内容传递给了网络服务中的方法,但我并不确定。我能得到的任何帮助都会很棒,我是一个初学者,并且一直在我的头上撞到墙上。

JSON(来自fiddler):

{"sampleIds":["1"],"line1":"","line2":"","line3":"","labelType":"Soil","startingLabelPosition":1}

使用Javascript:

var labelInfo = new Object();
labelInfo.sampleIds = sampleIds;
labelInfo.line1 = $('#ddlGrower').val();
labelInfo.line2 = $('#ddlFarm').val();
labelInfo.line3 = $('#ddlField1').val();
labelInfo.labelType = $('#ddlSoilLoginMatrix option:selected').text();
labelInfo.startingLabelPosition = parseInt($('#labelSelection').text());

$.ajax({
    type: "POST",
    url: "DesktopModules/DataManagement/TestService.svc/CreateLabelPdf",
    data: JSON.stringify(labelInfo),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data) {
        window.open(data.d);
    },
    error:  function(msg) {
        alert("Error: " + msg.status);
    }
});

C#网络服务:

[OperationContract]
public string CreateLabelPdf(List<string> sampleIds, string line1, string line2, string line3, string labelType, int startingLabelPosition)
{
   List<LabelContent> labels = new List<LabelContent>();

   foreach (var sample in sampleIds)
   {
      LabelContent labelContent = new LabelContent();
      labelContent.Line1 = line1;
      labelContent.Line2 = line2;
      labelContent.Line3 = line3;
      labelContent.LabelId = sample;
      labels.Add(labelContent);
   }   

   Creator creator = new Creator
   {
      IncludeLabelBorders = false
   };
   string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
   creator.PrintLabels(labels, new Avery5160(), path, startingLabelPosition);
   return path;
}

2 个答案:

答案 0 :(得分:0)

JSON似乎无效。我认为它应该从:

开始
{"sampleIds":["1"],"line1":"","line2":"","line3":"","labelType":"Soil","startingLabelPosition":1}

现在sampleIds是这样的:

{"sampleIds:["1"],"line1":"","line2":"","line3":"","labelType":"Soil","startingLabelPosition":1}

也就是说,"sampleIds"没有正确格式化为密钥。

答案 1 :(得分:0)

所以我终于明白了。我调试了代码并发现错误在这一行:

string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");

它得到一个空引用错误,所以我将代码更改为:

string path = System.Web.Hosting.HostingEnvironment.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");

虽然有效但我需要将相对字符串返回到浏览器而不是物理路径,所以我投入了:

string relativePath = @"\DesktopModules\DataManagement\Pdf\0.pdf";
return relativePath;

结尾代码如下:

string path = System.Web.Hosting.HostingEnvironment.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");

creator.PrintLabels(labels, new Avery5160(), path, startingLabelPosition);

string relativePath = @"\DesktopModules\DataManagement\Pdf\0.pdf";
return relativePath;