Copy link to clipboard
Copied
Hello Community,
I am a beginner in developing acrobat functionalities. I have written a folder level script to make a submenu in adobe acrobat pro which when pressed makes a POST request to a dummy server. I have been able to make GET requests without no problem but for POST requests I keep getting the following error :
"NotAllowedError: Security settings prevent access to this property or method" in the following code which I have taken from https://gist.github.com/randycasburn/802f278c3cfef0873dd086c34bbb9fee to make post requests. Any kind of help regarding this would be highly appreciated.
Thanks.
Edit : After reading some responses I have edited to make those required changes. But still, I am getting error.
Here is the code:
// Make HTTP POST request ajax = app.trustedFunction(function(fURL,doc) {
// Create some Stream object:
strObj = doc.createDataObject("myData", "This is the data"); app.beginPriv(); Net.HTTP.request({ cVerb:"POST", cURL:fURL, oRequest: strObj, oHandler: ajaxCallback}); app.endPriv(); }); // process the response ajaxCallback = { response:function(msg,uri,e){ var stream = msg; var string = ""; var error = e == undefined? 'No HTTP errors' : "ERROR: " + e.toSource(); string = SOAP.stringFromStream( stream ); oResult = JSON.parse(string); console.println(error); console.println( "id: " + oResult.id); console.println( "userId: " + oResult.userId); console.println( "title: " + oResult.title); console.println( "body: " + oResult.body); } }; // Add menu item to kick it all off app.addMenuItem({ cName: "Get Mock Data", cParent: "File", cExec: 'ajax("https://jsonplaceholder.typicode.com/posts/1", this);', nPos: 0});
the "doc.createDataObject" function does not return a stream object. Please refer to the Acrobat JavaScript Reference for basic info on all Acrobat JavaScript Objects and functions.
There are several ways to create a stream. The easiest is to use the "util.streamFromString()" function. Again, look it up in the Reference:
A
...Copy link to clipboard
Copied
Folder-level code can't refer to a document using "this" (or any other way), unless it is loacted inside a function and is called after the document has been opened. The folder-level code is executed before any document is opened in the application.
Copy link to clipboard
Copied
Snap!
Copy link to clipboard
Copied
The object "this" has different meanings (per the documentation) according to the context it is run. createDataObject is defined as part of the document object. Yet, you try to use this.createDataObject in a context where "this" is certainly not going to be a document - what document could it be?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
It's not a workaround, it's doing something entirely different. At the time your code runs there is no document in existence so it is impossible to use this method. You have to create it later. One way would be to pass a document object to your ajax function and use it there. And read about "this" thoroughly, since you are programming in this advanced way.
Copy link to clipboard
Copied
Before looking for "workarounds" you need to better explain what you're trying to achieve.
Why are you trying to create a new data object? How is that related to your SOAP request?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
What want you post to the server?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
the "doc.createDataObject" function does not return a stream object. Please refer to the Acrobat JavaScript Reference for basic info on all Acrobat JavaScript Objects and functions.
There are several ways to create a stream. The easiest is to use the "util.streamFromString()" function. Again, look it up in the Reference:
And the correct notation for a stream object is "stm"
var stmObj = util.streamFromString("This is data");
Copy link to clipboard
Copied
Okay, so when I created the stream object using util.streamFromString("this is the data") and removed the extra argument 'doc' from ajax fn and tried to execute it, it showed an error: {error:503, text: "a network connection could not be created", type: "SOAPError" }. Then I tried using SOAP.streamFromString(), but it still shows the same error.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Okay, so never mind I got it. The link I was using to make post request accepts only get requests. When I changed the link I was able to make post requests. Thanks!
Copy link to clipboard
Copied
Details, details, it's always the details 😉
Copy link to clipboard
Copied
@nishant13 can you post example of your post or get script used in your document? I'm trying to resolve the same issue.
Copy link to clipboard
Copied
And for testing purposes you don't need a menu item. Just run it from the Console Window, with no document open.
But if you do need a menu item, or an open document, use this code.
app.addMenuItem({ cName: "Get Mock Data", cParent: "File",
cExec: 'ajax("https://jsonplaceholder.typicode.com/posts/1", event.target)'});
event.target points to the target of the app level event, which is the current document.
Copy link to clipboard
Copied
And one more thing, Acrobat doesn't automatically add all the headers that are needed. At a minimum "Content-Type" should be added.
Copy link to clipboard
Copied
You've said you're a beginner. That's good to know and we want to help. But you're a beginner trying to do a pretty advanced task. Copy/pasting won't do it, days of reading the documentation and testing stuff lies ahead. Good luck!
Copy link to clipboard
Copied
Consider getting this tool. It will move you very quickly down the road to a solution.
https://www.pdfscripting.com/public/HTTP-Access-Tester-Description.cfm