POST request to the backend service

4462
25
03-05-2018 03:40 PM
MansiShah
New Contributor III

Hello,

I am modifying an existing Web App Builder template and in order to complete the functionality of a widget I need it to make a connection to a backend service. When I pull up the service url, a form comes up that needs to be filled. I have the information for the form stored in a JSON. I want to pass that to the backend but I am getting errors on how to make the POST request. 

Here is what I've tried inside of a function: 

  $.ajax({
                method: "POST",
                url: "url to the backend service",
                data: jobIds,
                dataType: "json",
                success: function(){
                    console.log("Success!");
                }
            });
I have also tried to add the "dojo/request" and "dojo/json" dependencies but those give me errors and won't even let the widget start.
Have anyone else encountered this? Or is there another way to POST request and get a response from a backend service different from what I've coded up?
Thank you!!
0 Kudos
25 Replies
RobertScheitlin__GISP
MVP Emeritus

Mansi,

  Is your backend service a RESTfull service or a SOAP based service?

0 Kudos
MansiShah
New Contributor III

Robert,

It is a REST service.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

I don’t use jquery Ajax like you are so I am not sure what your issue is. I use esriRequest to make restful calls to services. I would try switching to esriRequest. There are samples in the api you can follow.

0 Kudos
MansiShah
New Contributor III

Oh okay. I am using this site (request | API Reference | ArcGIS API for JavaScript 4.6 ) to look at the sample, but that is giving me errors. I am trying to do a POST request whereas the example I see is focused on getting data back from a server. If the POST request succeeds, there should be a response that's send back but I am not sure how to create that in code. What I have right now is: 

var createdJob = esriRequest(url, {
                "content" : jobIds,
                "callbackParamName": "callback",
                "usePost" : true,
                "handleAs" : "json"
            }).then(function(response){
                console.log("The response is: ", response);
            });
Can you see what I might be doing wrong by any chance?? The error is get is a "Uncaught TypeError: Cannot read property 'trim' of undefined" error.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mansi,

   I thought your service requires input parameters? If so you need to provide those to the esriRequest using query parameter.

If the request URL points to a web server that requires parameters, specify them here

 

0 Kudos
MansiShah
New Contributor III
var createdJob = esriRequest(url, {
                "query" : jobIds,
                "callbackParamName": "callback",
                "usePost" : true,
                "handleAs" : "json"
            }).then(function(response){
                console.log("The response is : ", response);
            });
createdJob.then(
                function(data){
                    console.log("Success: ", data)
                },
                function(error){
                    console.log("Error!!");
                }
            );
This still gives me the same error: "Uncaught TypeError: Cannot read property 'trim' of undefined" ??
RobertScheitlin__GISP
MVP Emeritus

Mansi,

   And where in your code is this error breaking?

0 Kudos
MansiShah
New Contributor III

When you open up the widget, there is a form for you to fill out. After you fill out the form, you have to hit the "Create Job" button. When you hit that, it triggers the execution of a certain function and inside that function is where I am putting this code in. 

P.S. - I do have the dependencies ("esri/request", "esri/config", esriRequest, esriConfig) included in my code. 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Mansi,

  I doubt that a trim error is coming from the esriRequest portion of the code.

0 Kudos