2

I'm trying to use this code with Parse.Cloud.beforeSave...

var script = document.createElement('script');

and I get this: Result: ReferenceError: document is not defined

How can I use document with Parse.Cloud?

My code:

function getJSONP(url, success) {
        var script = document.createElement('script');
        script.src = url.replace('callback=', 'callback=do');
        document.head.appendChild(script);

        do = function (data) {
            success(data);
        }
 }
5
  • 2
    Parse Cloud code does not run in the browser. It runs on the server where the DOM does not exist. Why are you trying to use the DOM? Commented Jun 23, 2015 at 5:31
  • I'm trying to download a JSONP... function getJSONP(url, success) { var script = document.createElement('script'); script.src = url.replace('callback=', 'callback=do'); document.head.appendChild(script); do = function (data) { console.log(data); success(data); } } Commented Jun 23, 2015 at 5:35
  • Not sure if Parse allows it, but you could just make a normal HTTP request and eval the response. JSONP is only used to circumvent the same-origin policy in the browser. There is no need for script tags on the server side, just make the HTTP request. Commented Jun 23, 2015 at 5:39
  • I wonder if you can somehow use jsdom on Parse.com? Alternatively, does this question and answer help? Commented Jun 23, 2015 at 6:16
  • Thanks! @FelixKling the solution is Parse.Cloud.httpRequest. Commented Jun 23, 2015 at 6:41

1 Answer 1

0

The solution:

Parse.Cloud.httpRequest({
    url: myURL,
    success: function(httpResponse) {
        console.log(httpResponse.data);
        response.success();
    }, error: function(httpResponse) {
        response.error('Request failed...');
     }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.