I needed to access a REST web service from jQuery, but Chrome would throw an error during the ajax call due to the "origin" policy. It's possible to setup a CORS filter with Tomcat and Apache, but that sounded like a lot of work.
Instead, if you can use PHP, just download the following PHP proxy:
https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/proxy.php
Two changes are needed:
Instead, if you can use PHP, just download the following PHP proxy:
https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/proxy.php
Two changes are needed:
- Edit line 176 such that it reads $url_query_param = 'url';
- Either fix the regexp at lines 172 and 173 which checks that the call is to sales force.com (set it to match your website) or comment out lines 206 to 212 (potentially dangerous).
var req = $.ajax({ type: 'GET', contentType: 'application/json', mimeType: 'application/json', url: 'http://proxy-server/app/proxy.php?mode=native&url=http://api-server/api/object/'+$("#objectID").val(), dataType: 'json', success: function(data, textStatus, jqXHR) { alert("Got data successfully"); $('#responseData').text(JSON.stringify(data)); }, error: function(xhr, textStatus, error) { alert("Error: " + textStatus); } });
That's it!
1 comment:
Post a Comment