Printing page from AJAX

时间:2017-12-18 08:32:35

标签: javascript php ajax

I wrote a simple generator to create offers. It works good, but I got the problem with printing. When I try a print page from AJAX request, I getting index page not data from AJAX request.

What's wrong?

Additionally, data from AJAX is correct.

org.sqlite.SQLiteException: [SQLITE_LOCKED]  A table in the database is locked (database table is locked)
    at org.sqlite.core.DB.newSQLException(DB.java:909)
    at org.sqlite.core.DB.newSQLException(DB.java:921)
    at org.sqlite.core.DB.execute(DB.java:822)
    at org.sqlite.core.CoreStatement.exec(CoreStatement.java:75)
    at org.sqlite.jdbc3.JDBC3Statement.execute(JDBC3Statement.java:61)
    at Storage.OfflineDB.dropTables(OfflineDB.java:256)
    at Persistency.SyncMapper.syncDatabases(SyncMapper.java:56)
    at Persistency.SyncMapperTest.testSyncDatabases(SyncMapperTest.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:38)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:535)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1182)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:1033)

Example for PHP file :

$.post({
    type: "POST",
    url: "generate.php",
    data: {pid: pid, net: net, brutto: brutto, contractor: contractor, delivery: delivery, term: term},
}).done(function(data) {
    window.print(data);
});

2 个答案:

答案 0 :(得分:5)

print()方法不带任何参数。因此,如果要从变量打印数据,可以使用以下方法:

$.post({
    type: "POST",
    url: "generate.php",
    data: {pid: pid, net: net, brutto: brutto, contractor: contractor, delivery: delivery, term: term},
}).done(function(data) {
    printWindow = window.open('');
    printWindow.document.write(data);
    printWindow.print();
});

这将打开一个新标签,屏幕上的值为data,可以打印。

答案 1 :(得分:3)

The print() method:

Opens the Print Dialog to print the current document.

It doesn't take any arguments (so any arguments you pass it will be ignored).

If you want to print data you have in a variable, you need to put it in the document the user is viewing.