You need to sign in to do that
Don't have an account?
Marco Mancilla
How to make an AJAX request for Report JSON data in a visualforce page from a static resource javascript file
Hello,
I have a javascript function in a visualforce page that makes an AJAX request for some report JSON data. The function works fine when it is embedded directly in the visualforce page. However, if I attempt to move it to a static resource outside the visualforce file, include that static resource in the VF file and call the function, it fails. The json data it returns says "INVALID_SESSION_ID" even though I passed the value of {!$Api.Session_ID} to my function. For some reason salesforce won't allow this to work. How can I make this work?
Here is the code that works:
Would appreciate some help with this.
Thanks.
I have a javascript function in a visualforce page that makes an AJAX request for some report JSON data. The function works fine when it is embedded directly in the visualforce page. However, if I attempt to move it to a static resource outside the visualforce file, include that static resource in the VF file and call the function, it fails. The json data it returns says "INVALID_SESSION_ID" even though I passed the value of {!$Api.Session_ID} to my function. For some reason salesforce won't allow this to work. How can I make this work?
Here is the code that works:
<apex:page sidebar="false"> <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" /> <script> var apiSid = '{!$Api.Session_ID}'; var report_id = 'my report id goes here' function getReportJsonData(apiSid, report_id) { console.log(apiSid); var jsonData = JSON.parse($.ajax({ beforesend: function(xhr) { xhr.setRequestHeader('Authorization', 'Bearer ' + apiSid); }, url: "/services/data/v29.0/analytics/reports/" + report_id, dataType: "json", async: false }).responseText); console.log(jsonData); return jsonData; } var jsonData = getReportJsonData(apiSid, report_id); </script> </apex:page>However if I move getReportJsonData() into a static resource, then try to call it like so:
<apex:page> <apex:includeScript value="{!$Resource.MapSettings}"/> <script> var apiSid = '{!$Api.Session_ID}'; var report_id = 'my report id goes here'; var jsonData = getReportJsonData(apiSid, report_id); </script> </apex:page>This fails, the json data returned says "INVALID_SESSION_ID".
Would appreciate some help with this.
Thanks.
Marco Mancilla
Nevermind, I got it working. There appears to have been an error in my code. Please disregard this post.
Marco Mancilla
Just to follow up, my mistake was that I misspelled "beforeSend" (it's case sensitive)