2011-10-31 27 views
0

我的問題是當我點擊按鈕調用「打印」它會從表中獲取數據並通過jQuery發送ajax調用頁面方法來創建pdf,但沒有發生。jQuery Ajax post創建PDF

這是我的代碼

客戶

function Print(button) { 
       var arrDTO = new Array(); 
       var printButton = $(button); 
       var $targetTable = printButton.parent().parent().find('table'); 
       var $tr = $targetTable.find('tbody tr'); 

       for (var i = 0; i < $tr.length; i++) { 
        var newObj = {}; 
        var td = $($tr[i]).find('td'); 
        newObj.BugID = $(td[0]).find('a').html().trim(); 
        newObj.Title = $(td[2]).find('a').html().trim(); 
        newObj.HouseName = $(td[3]).html().trim(); 
        newObj.RoomName = $(td[4]).html().trim(); 
        newObj.ThePersonResponsibleName = $(td[5]).html().trim(); 
        newObj.FAGName = $(td[6]).html().trim(); 
        newObj.CompanyName = $(td[7]).html().trim(); 
        newObj.Reminders = $(td[8]).html().trim(); 
        newObj.Delayed = $(td[9]).html().trim(); 
        newObj.Appendix = $(td[10]).html().trim(); 
        newObj.MoreInfo = $(td[11]).html().trim(); 
        newObj.DeadLine = $(td[12]).html().trim(); 
        newObj.Comment = $(td[13]).find('input[type=\"text\"]').val().trim(); 
        newObj.Retention = $(td[14]).find('label').html().trim(); 
        newObj.Discount = $(td[15]).html().trim(); 
        newObj.Balance = $(td[16]).html().trim(); 
        newObj.StatusName = $(td[17]).html().trim(); 

        arrDTO[i] = newObj; 

        //alert('row ' + (i + 1) + ' ' + arrDTO[i].BugID); 
       } 

       var DTO = { 'issueType2ViewModel': arrDTO }; 

       $.ajax({ 
        "type": "POST", 
        "url": "IssuesType2.aspx/Print", 
        "data": JSON.stringify(DTO), 
        "contentType": "application/json; charset=utf-8", 
        "dataType": "json", 
        "success": function (data) { 
         alert('success'); 
         //window.location = "/1.pdf"; 
        }, 
        "error": function (data) { 
         alert('Error'); 
        } 
       }); 
      } 

這是我的服務器代碼

[System.Web.Services.WebMethod()] 
     public static void Print(Old_App_Code.ViewModel.IssueType2ViewModel[] issueType2ViewModel) 
     { 
      //HttpContext.Current.Request[ 
      int projectID = 0; 
      int catID = 0; 

      if (HttpContext.Current.Request["pid"] != null) 
      { 
       projectID = Convert.ToInt32(HttpContext.Current.Request["pid"]); 

       if (HttpContext.Current.Request["Cat"] != null) 
       { 
        catID = Convert.ToInt32(HttpContext.Current.Request["Cat"]); 
       } 
      } 

       System.IO.FileStream fs = new FileStream(HttpContext.Current.Request.PhysicalApplicationPath + "\\" + "Test.pdf", FileMode.Create); 
       Document doc = new Document(PageSize.A4_LANDSCAPE, 1, 1, 1, 1); 
       PdfWriter writer = PdfWriter.GetInstance(doc, fs); 
       doc.Open(); 
       doc.Add(new Paragraph("Hello World")); 
       doc.Close(); 
       writer.Close(); 
       HttpContext.Current.Response.ContentType = "pdf/application"; 
       HttpContext.Current.Response.AddHeader("content-disposition", 
       "attachment;filename=First PDF document.pdf"); 
       HttpContext.Current.Response.Redirect("~/Test.pdf"); 
     } 

但與此沒有發生。我做什麼錯了?

回答

0

基於ajax調用,您至少應該獲得成功或錯誤。

如何嘗試調試您的服務器代碼。