0

I would like to alert(httpAccept) outside of the function but this is not possible. All actions must me made inside the httpAcceptResponse(data).

When I consol httpAccess after the getHTTP() I get

Resource interpreted as Script but transferred with MIME type text/html: "http://www.domain.com/httpaccept.php?callback=httpAcceptResponse".

What is a workaround for this?

function requestServerCall(url) {
  var head = document.head;
  var script = document.createElement("script");

  script.setAttribute("src", url);
  head.appendChild(script);
  head.removeChild(script);
}



var httpAccept = '';

function httpAcceptResponse(data) {
  httpAccept = data.token;
}

function getHTTP() {
  requestServerCall("http://www.domain.com/httpaccept.php?callback=httpAcceptResponse");
}

getHTTP();
1
  • 4
    There is none. You are making a JSONP call (or at least appending a script to the <head>). It's asynchronous, so you need to do anything related to the data in the callback. One way is to pass a callback when you request the data. Commented Aug 20, 2014 at 18:54

1 Answer 1

2

You need to bind a callback. Something like:

var httpAcceptResponse;
function getHTTP(callback) {
  httpAcceptResponse = callback;
  requestServerCall("http://www.domain.com/httpaccept.php?callback=httpAcceptResponse");
}

getHTTP(function(data) {
  console.log(data.token);
  // now you have the variable
});

Edit: If you want to use it globally, set it in the callback:

getHTTP(function(data) {
  window.httpAccept = data.token;
});

Note, however, that the variable is not available globally until the callback is called.

Sign up to request clarification or add additional context in comments.

4 Comments

oh thank you.. I have two questions, at the first example, the requestServerCall is executed 1 time only, correct? The second question for the second example, is, how do I print it outside? Because I tried alert(window.httpAccept) outside the function and got undefined
@Fataoulas: That's the issue here. You can't access it globally. Because it's being retrieved asynchronously. You need to wait until the call is done, and that's why you use callbacks. P.S. You may need to change your design if you are using asynchronous calls in JavaScript. It's a different paradigm.
@Fataoulas: "Because I tried alert(window.httpAccept) outside the function and got undefined" And we are back at square one: You are trying to read the value before the callback was called. While it is possible the access it "outside" you don't really know when you can access if you don't work with the callback.
@Fataoulas I think you are having an "xy problem". Instead of asking "how do I access it outside", try thinking about "when do I need it and how should I make sure it's available before that time". If you still need help, post a new question specifying what you're trying to do with the httpAccept variable.

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.