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
asadimasadim 

How to get response back from VF when using XMLHttpRequest?

Hi,

 

I have a VF page that simply outputs some text:

 

<apex:page controller="myController" contentType="text/javascript" showHeader="false">
some garbage text
</apex:page>

 Then I have a JavaScript that uses XMLHttpRequest to make a request to the above VF page:

 

var url = "http://na4.salesforce.com/apex/myPage";
xmlhttp.open('GET', url, true);
xmlhttp.send(null);

 Before making the request I make sure that I'm logged in with my salesforce account (so that I don't need to pass user/pass thru the url). The problem is that, I never get anything back. xmlhttp.status is always 0 and xmlhttp.responseText is always blank. Firebug shows the status of the GET request as 200 OK.

 

Any ideas how I should make REST calls to a VF page and get the response back? Thanks!!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
stephanstephan

I haven't tested this, but I suspect the issue may be that you need to URL encode the startURL param. Without doing so, param2 and param3 would be interpreted as params for login.jsp, and not for your myPage.

All Answers

stephanstephan

Could you include the full javascript for the page making the request? Your example seems incomplete.

 

Also, where is the page making the request hosted? It is a VF page as well? If it's on a different domain you could be running into a cross domain issue.

 

...stephan

asadimasadim

I've switched to dojo and this is what I have:

 

 

			        dojo.xhrGet( {
				        // The following URL must match that used to test the server.
				        url: "https://www.salesforce.com/login.jsp?un=<username>&pw=<pass>&startURL=/apex/myPage", 
				        handleAs: "text",
				
				        timeout: 5000, // Time in milliseconds
				
				        // The LOAD function will be called on a successful response.
				        load: function(response, ioArgs) {
				          alert(response);
				          return response;
				        },
				
				        // The ERROR function will be called in an error case.
				        error: function(response, ioArgs) {
				          console.error("HTTP status code: ", ioArgs.xhr.status);
				          return response;
				          }
			        });

 

 

I don't get anything back. This works however if instead of the salesforce url I pass in the path to some text file on my local machine.

stephanstephan

Again -- this could be a cross domain issue. Generally the browser only lets you make an XHR request from/to the same domain.

 

You may want to try this in Firefox with Firebug installed to see if any errors show up in the console.

 

...stephan

asadimasadim

You're right, that was an xdomain issue. So I'm now using dojox.io.windowName.send but now there's a new issue. My controller doesn't seem to get called when I pass the following url to dojox.io.windowName.send:

 

https://www.salesforce.com/login.jsp?un=<user>&pw=<pass>&startURL=/apex/myPage?param1=xyz&param2=abc&param3=asd

 

 

This call basically returns me the content of my VF page without the controller ever running (I know this because in the constructor of the controller I set some value on the page that doesn't show up in the callback result).

 

Any thoughts on that?

stephanstephan

I haven't tested this, but I suspect the issue may be that you need to URL encode the startURL param. Without doing so, param2 and param3 would be interpreted as params for login.jsp, and not for your myPage.

This was selected as the best answer
asadimasadim

I think you're right. Thanks!!