Making 'POST' request using Javascript

Explorer ,
Jun 01, 2020

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});

 

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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:

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2Futil_methods.htm%23TOC_streamFromString1bc-9&rhtocid=_6_1_8_78_0_8

 

And the correct notation for a stream object is "stm"

var stmObj = util.streamFromString("This is data");

TOPICS
Acrobat SDK and JavaScript

Views

621

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Making 'POST' request using Javascript

Explorer ,
Jun 01, 2020

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});

 

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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:

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2Futil_methods.htm%23TOC_streamFromString1bc-9&rhtocid=_6_1_8_78_0_8

 

And the correct notation for a stream object is "stm"

var stmObj = util.streamFromString("This is data");

TOPICS
Acrobat SDK and JavaScript

Views

622

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 01, 2020 0
Most Valuable Participant ,
Jun 01, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Most Valuable Participant ,
Jun 01, 2020

Copy link to clipboard

Copied

Snap!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Most Valuable Participant ,
Jun 01, 2020

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Explorer ,
Jun 01, 2020

Copy link to clipboard

Copied

So what is the workaround for the above code to work?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Most Valuable Participant ,
Jun 01, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Most Valuable Participant ,
Jun 01, 2020

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Explorer ,
Jun 01, 2020

Copy link to clipboard

Copied

Actually I am trying to make a simple POST request for a demonstration purpose. I searched online and found an approach on the link provided above in the post. My end goal is to execute a simple POST request using javascript. I don't know what is the use of the stream object here. I have mentioned that I am a beginner.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Adobe Community Professional ,
Jun 01, 2020

Copy link to clipboard

Copied

What want you post to the server?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Explorer ,
Jun 01, 2020

Copy link to clipboard

Copied

I am trying to post data using stream object (strObj).

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Adobe Community Professional ,
Jun 01, 2020

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:

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_Acro...

 

And the correct notation for a stream object is "stm"

var stmObj = util.streamFromString("This is data");

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 1
Explorer ,
Jun 01, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0
Adobe Community Professional ,
Jun 02, 2020

Copy link to clipboard

Copied

What happens when you use a GET request?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 02, 2020 0
Explorer ,
Jun 02, 2020

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 02, 2020 1
Adobe Community Professional ,
Jun 02, 2020

Copy link to clipboard

Copied

Details, details, it's always the details 😉

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 02, 2020 0
Adobe Community Professional ,
Jun 01, 2020

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 01, 2020 0