0

i set up a webservice thats cross domain and needs to be contacted via json with padding on a simple jquery codeline like this, i am successfull getting back json data.

$.getJSON("http://server/series/hist?jsonp=?", function(data){
 console.log(data);
});

the webservice, will wrap the result in a function, whenever "jsonp" exists within in the url. for those cases i used a default function name like:

myfunction({"a":1})

jquery helps me out here, and trys to call the function, that isnt existing ("myfunction()"). what i am trying to achieve instead is a simple call of the callback function (see above), to handle the data locally.

can you point me in the right direction?

thank you

1 Answer 1

3

I'm not quite sure what your problem actually is, but:

Interpretation 1

Assuming that by "locally" you mean "without using a callback":

That is impossible. JSON-P cannot work synchronously as it depends on the addition of a <script> element (which won't be processed until the current function has finished executing).

Interpretation 2

Assuming that by that isnt existing ("myfunction()") you mean "Your webservice always uses the function name myfunction:

Fix the webservice. jsonp=? means "Randomly generate a function name and pass it as the jsonp parameter.

The webservice must use that parameter to determine the function name used, and not use a fixed value such as myfunction.

Interpretation 3

You don't want to use JSON-P as the input, but to call your anonymous function directly.

You can't. It isn't stored anywhere you can access it. You have to rewrite your code so it isn't passed directly to getJSON:

function myFunction(data){
  console.log(data);
}

$.getJSON("http://server/series/hist?jsonp=?", myfunction); 
myfunction({"a":1})
Sign up to request clarification or add additional context in comments.

4 Comments

well by locally i meant the callback function of my code above: function(data){ console.log(data);} since it is mentioned in every JSON example
i just read your edit Interpretation2. if i understand you right, jquery randomly generates a name for the "?". so the webservice has to catch that and wrap it around the json result in order to get the callback function to work?
@zbug — Yes. You couldn't use the same name every time, you'd get conflicts.
ok perfect. i could figure it out. the webservice didnt accept any GET values containing "_" within the string. jquery on the other hand does that. now my inner callback function just works fine. your help is appreciated

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.