打印json数据,结合返回的整个数据

时间:2014-07-25 09:29:25

标签: jquery ajax json asp.net-mvc-4 printing

这是我的代码

 public ActionResult PrintBarcodeLabel1(string data)
    {
      zplstr = zplstr + Environment.NewLine + zpl;
      return Json(zplstr, JsonRequestBehavior.AllowGet);
    }

这里将返回

^ XA

^ FT90,85

^ CI0

^ A0N,38,52 ^ FDAsset No。:^ FS

^ FT375,85

^ A0N,38,52 ^ FDSave ^ FS

^ FT90,173

sdfas ^ FS

^ XZ

- 环境新线,新线及以下

^ XA

^ FT90,85

^ CI0

^ A0N,38,52 ^ FDAsset No。:^ FS

^ FT375,85

^ A0N,38,52 ^ FDSave ^ FS

^ FT90,173

sdfas ^ FS

^ XZ

像这样

但是,当我将它附加到div或当我打印数据是绑定或打印像这样

^ XA ^ FT90,85 ^ CI0 ^ A0N,38,52 ^ FDAsset编号:^ FS ^ FT375,85 ^ A0N,38,52 ^ FDSave ^ FS ^ FT90,173 ^ A0N,38,52 ^ FDAsset名称:^ FS ^ FT375,173 ^ A0N,38,52 ^ FDSave ^ FS ^ FT90,261 ^ A0N,38,52 ^ FDSerial No.:^FS ^ XA ^ FT90,85 ^ CI0 ^ A0N,38,52 ^ FDAsset编号:^ FS ^ FT375,85 ^ A0N,38,52 ^ FDSave ^ FS ^ FT90,173 ^ A0N,38,52 ^ FDAsset名称:^ FS ^ FT375,173 ^ A0N,38,52 ^ FDSave ^ FS ^ FT90,261 ^ A0N,38,52 ^ FDSerial No。:^ FS

这是我的ajax打印

var newWin = window.open();
        $.ajax({
            type: "POST",
            async: true,
            url: '/BarcodePrintTest/PrintBarcodeLabel1',
            data: JSON.stringify(dataToSend),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                debugger
                if (data != "") {
                   // $('#my_cars_compare').html(data);
                    newWin.document.write('</head><body >');
                    newWin.document.write(JSON.stringify(data));
                    newWin.document.write('</body></html>');
                    newWin.document.close();
                    newWin.focus();
                    newWin.print();
                    newWin.close();
                    window.location = '@Url.Action("Create", "BarcodePrintTest")';
                }

            },

3 个答案:

答案 0 :(得分:1)

您可以使用<br />来表示html中的换行符。

而不是

zplstr = zplstr + Environment.NewLine + zpl;

你可以使用

zplstr = zplstr + "<br />" + zpl;

谢谢!

答案 1 :(得分:0)

尝试返回System.Web.Mvc.JsonResult而不是ActionResult

答案 2 :(得分:0)

我终于完成了这个

$('#my_cars_compare').html(JSON.stringify(data.replace(/\r\n/g, "<br/>")));//.replace(/\r/g, "<br/>")
    newWin.document.write('</head><body >');
    newWin.document.write(JSON.stringify(data.replace(/\r\n/g, "<br/>")));
    newWin.document.write('</body></html>');

我转换为JSON.stringify ...其中我得到了\ r \ n的分隔符最后我用br标签替换了\ r \ n

感谢您的所有回复