• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Making 'POST' request using Javascript

Explorer ,
Jun 01, 2020 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});

 

TOPICS
Acrobat SDK and JavaScript

Views

2.9K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 01, 2020 Jun 01, 2020

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

 

A

...

Votes

Translate

Translate
Community Expert ,
Jun 01, 2020 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.

Votes

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
community guidelines
LEGEND ,
Jun 01, 2020 Jun 01, 2020

Copy link to clipboard

Copied

Snap!

Votes

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
community guidelines
LEGEND ,
Jun 01, 2020 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?

Votes

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
community guidelines
Explorer ,
Jun 01, 2020 Jun 01, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
LEGEND ,
Jun 01, 2020 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.

Votes

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
community guidelines
Community Expert ,
Jun 01, 2020 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?

Votes

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
community guidelines
Explorer ,
Jun 01, 2020 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.

Votes

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
community guidelines
Community Expert ,
Jun 01, 2020 Jun 01, 2020

Copy link to clipboard

Copied

What want you post to the server?

Votes

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
community guidelines
Explorer ,
Jun 01, 2020 Jun 01, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Expert ,
Jun 01, 2020 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");

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines
Explorer ,
Jun 01, 2020 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.

Votes

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
community guidelines
Community Expert ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

What happens when you use a GET request?

Votes

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
community guidelines
Explorer ,
Jun 02, 2020 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!

Votes

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
community guidelines
Community Expert ,
Jun 02, 2020 Jun 02, 2020

Copy link to clipboard

Copied

Details, details, it's always the details 😉

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines
New Here ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

LATEST

@nishant13 can you post example of your post or get script used in your document? I'm trying to resolve the same issue.

Votes

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
community guidelines
Community Expert ,
Jun 01, 2020 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. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines
Community Expert ,
Jun 01, 2020 Jun 01, 2020

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. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines
LEGEND ,
Jun 01, 2020 Jun 01, 2020

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!

Votes

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
community guidelines
Community Expert ,
Jun 01, 2020 Jun 01, 2020

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

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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
community guidelines