1

Not sure if this should work but from a jquery ajax request with the response I want to evaluate the script being passed back. Here goes for an example.

$.ajax({
    url: some/path,
    dataType: 'script'
});

Then with my response I'm using php to spit out the javascript.

<?php
header("content-type: application/javascript");

$str = <<<EOF
    alert('help');
EOF;
echo $str;

If I'm correct I should get a alert pop-up after performing the request.

I can see from the response headers it correct:

Accept:application/json, text/javascript, */*; q=0.01 

And response is:

alert('help');

But no alert box is being triggered. Am I missing something or can this not be done this way?

Hope you can advise.

TY

1 Answer 1

1

You should call success function in $.ajax

and execute the response

page1.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" 
type="text/javascript"></script>

<script>

  $.ajax({

      url: "page2.php",

      dataType: 'script',

      success: function(resp) {

          resp;  //this is your response which is contains alert('help')

      }

    });

</script>

page2.php

<?php

header("content-type: application/javascript");

$str = <<<EOF
    alert('help');
EOF;

echo $str;

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

3 Comments

@Lee: May I know your feedback
I tried with success which I thought also was not necessary. but didn't work either.
I worked it out. I was using fileupload plugin which works a little strange. all good and my code works.

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.