developerforce / Force.com-JavaScript-REST-Toolkit

ForceTK - a minimal Force.com REST API for JavaScript apps
BSD 3-Clause "New" or "Revised" License
315 stars 175 forks source link

Call Apex REST POST Method Error #67

Closed bruceyue closed 9 years ago

bruceyue commented 9 years ago

returned this 'net::ERR_CONNECTION_RESET'

Here is my code:

 var url = '/services/apexrest/namespace/DownloadService/';
 tkClient.apexrest(
                url,
                function(response){
                    console.log(Base64.decode(response));
                    var responseJson = Base64.decode(response);
                },
                function(error){
                    console.log(error);
                },
                "POST",
                null,
                '{"jsonData":"eyJvYW1lAwMDAwWVNJbUFBTyJ9"}',
                false
            );
metadaddy commented 9 years ago

How are you creating tkClient? Can you post that code. Also, what do you see in the JavaScript Console?

bruceyue commented 9 years ago

I do it like this

   var tkClient = new forcetk.Client();
   tkClient.setSessionToken('{!$Api.Session_ID}');

When I call GET method, it can success.

bruceyue commented 9 years ago

I think there is a bug for this in forcetk apexrest method. How should I pass the paramMap params?

 for (paramName in paramMap) {
            xhr.setRequestHeader(paramName, paramMap[paramName]);
  }
bruceyue commented 9 years ago

. 66df3720-b066-43b3-89ba-6f4f842dcb7b This is the error form console

metadaddy commented 9 years ago

I think I see what it is - the first argument to apexrest should be the path relative to /services/apexrest, and your JSON should be the payload, so you should call it like:

 var url = '/namespace/DownloadService/';
 tkClient.apexrest(
            url,
            function(response){
                console.log(Base64.decode(response));
                var responseJson = Base64.decode(response);
            },
            function(error){
                console.log(error);
            },
            "POST",
            '{"jsonData":"eyJvYW1lAwMDAwWVNJbUFBTyJ9"}'
        );

paramMap is if you want to pass extra HTTP headers. You don't need to set it or the retry flag

bruceyue commented 9 years ago

image

Yes, it works for fetching small data, but for large data, this error occurred. How to ffix it.

metadaddy commented 9 years ago

OK - the problem here is that Apex REST calls still have to go through the Ajax Proxy (unlike the Force.com REST API, Apex REST is not exposed on the Visualforce hostnames - the Winter '16 release might fix this #safeharbor). The Ajax Proxy has a fixed 10 second timeout, so long running operations will fail.

Right now, the only way to do this is to deploy your own proxy with CORS support, or figure out a way to use the REST API.

leordev commented 7 years ago

@metadaddy is there a way to fork forcetk and change the 10 second timeout? can you throw a file that I should start looking to change that?

metadaddy commented 7 years ago

@leordev If you're using the Ajax proxy, you're stuck with the 10 second timeout - that's in the proxy service itself at Salesforce. You may not need to even use the proxy, though. Try disabling it and see if it works - see http://salesforce.stackexchange.com/a/13171/67

leordev commented 7 years ago

@metadaddy oh I see it... it's on the server side, not on the client. yeah, the problem is that I'm not using the APEX Rest Data service. I'm using only custom APIs that I created in the environment! Thank you!