function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kevin LanguedocKevin Languedoc 

Call Apex Webservice from Javascript in VF page

I need to test the communication between javascript in a VF page that is rendered from a button on a open case document and an Apex webservice that is supposed to send an e-mail. However I get the impression that the web service is never called. So I created a small dummy webservice, called WSReturn to receive a name and return a string. I am trying to dtermine if the problem is in the javascript code or the Apex code.

Browser: Chrome, Version 42.0.2311.90 (64-bit) on a Mac
The javascript function sayHelloService is called on the window.onload event. However I am getting this error message:

Refused to set unsafe header "User-Agent"
send @ connection.js:593
sforce.SoapTransport.send @ connection.js:1012
sforce.Connection._invoke @ connection.js:1727
sforce.Apex.execute @ apex.js:60
sayHelloService @ Form1747web?scontrolCaching=1&id=50055000000fkiR&isdtp=vw&nonce=dcb74e9d3d4fd43461bc263b172a1a02c72…:41

Javascript code:
function sayHelloService(){
                  var hello = sforce.apex.execute("WSReturn", "Hello", {name:'Kevin'}); 
                  sforce.debug.log(hello);
               }



Apex Code:
global class WSReturn {
    
    webservice static String Hello(String name){
        String sayHello;
        
        sayHello = 'Hi ' + name;
        System.debug('sayHello');
        
        return sayHello;
        
        
    }

}

 
Best Answer chosen by Kevin Languedoc
James LoghryJames Loghry
Why not use remote actions instead for this use case?  For instance, set your web service class as an extension controller (will likely have to add an additional constructor to the class), but then you can call the webservice method via javascript pretty easily.  This is what it would like like as a matter of fact:
 
WSReturn.Hello(name,function(response){ //callback });

You should be able to still use it as a webservice for other use cases as well in this scenario.

All Answers

Kevin LanguedocKevin Languedoc
I have ready different post that point to Chrome as the culprit, but with older versions. The WSReturn web service worked fine. So this issue is related to Chrome (Mac). I haven't tried on a Windows machine or other browsers.

 
James LoghryJames Loghry
Why not use remote actions instead for this use case?  For instance, set your web service class as an extension controller (will likely have to add an additional constructor to the class), but then you can call the webservice method via javascript pretty easily.  This is what it would like like as a matter of fact:
 
WSReturn.Hello(name,function(response){ //callback });

You should be able to still use it as a webservice for other use cases as well in this scenario.
This was selected as the best answer
Kevin LanguedocKevin Languedoc
Hi James

I didn't think of that. That is a great solution.

Thanks
Kevin