4

I'm trying to send an JSONP Ajax request (from client.htm to proxy.htm), which is received by a proxy page which creates another different Ajax JSONP request to a service (proxy.htm ---> service.php). At the moment the proxy page receives the JSON format object from the service, but I'm stuck on returning the object to the first initial Ajax request. The following if the sample code I'm trying to run. Is this possible with Javascript? If so, how could I return the object to the clientCallback function.

Client.htm

// CLIENT ---> PROXY
$(document).ready(function () {
     $.ajax({
            type: 'GET',
            url: 'http://localhost/jsonp_proxy/listener.htm',
            dataType: 'jsonp',
            jsonpCallback: "clientCallback",
            success: function (theData) {
              alert("Success");
            },
            error: function (xhr, ajaxOptions, thrownError) {
              alert("Error");
            }
        });
      });
      window.clientCallback = function(results) {
        alert(results.uid);
      }

Proxy.htm

      // PROXY ---> SERVER
      $(document).ready(function () {
        $.ajax({
          type: 'GET',
          url: 'http://localhost/jsonp_server/service.php',
          dataType: 'jsonp',
          jsonpCallback: "proxyCallback",      
          success: function (theData) {
//          alert("Success");
          },
          error: function (xhr, ajaxOptions, thrownError) {
//          alert("Error");
          }
        });
      });
      window.proxyCallback = function(resultData) {
        // HOW TO CALL CLIENT CALLBACK FUNCTION
      }

Service.php

<?php
echo "proxyCallback("."{'uid': 123, 'username': 'jdirt', 'name': 'Joe Dirt'}".");";
?>

2 Answers 2

1

One of the few semi-valid uses for eval:

success: function (theData) {
  eval("(" + theData + ")");
  //alert("Success");

Just as a reminder, JSONP is something of a hack and security risk. You are giving a lot of trust and control to a 3rd party.

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

4 Comments

Thanks for reply but this does not make it back to the client.htm page. The Ajax goes into the error function.
Never use eval(). Use JSON.parse().
@saml -- Normally I'd agree but JSONP is slightly different beast from JSON. JSONP is executable code that must be evaled because it contains an internal callback handler.
@JeremyJStarcher JSONP requests will not ever return the source to you other than by passing some parameter to a function you're requested it to. You can't eval anything, because the success and failure callbacks will only get triggered when the browser finishes loading the code from the remote server, even if the callback itself throws an exception.
0

Simply call window.clientCallback from window.proxyCallback.

4 Comments

"does not work" is an extremely vague way to describe failure. What happened? What about what happened did not work the way you expected it? What did you actually try?
Ok, so I tried calling window.clientCallback from window.proxyCallback but when I browse client.htm the request goes into the error function of the request. I tried calling the functions without making them global and I got the same result. Calling the proxyCallback from the PHP page works fine, but I cannot seem to find a way to do the same with Javascript
Why are you using jsonp for this?
In large part to an existing service that was slightly modified. The actual service is written in C#, and came with example web page that uses JSONP to get data from the service. Note that the web page and service are complete but due to design (out of my control) I find myself attempting to make this work. It seems to not be the best approach based on the your question and other replies, is this correct? If so, what is a better approach?

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.