Dr Casper Black
Dr Casper Black

Reputation: 7468

Ajax call to a method that prints javascript

Ok,

I have a some javascript code in the database

Table: jsSnippets

Field: snippet

Type: Text

<SCRIPT SRC="https://svc.com/somestuff.js"></SCRIPT>
<script>
var fubar = 'stuf'
send_some_stuf_to_svc(fubar) // sends some data to a service :)
</script>

So i have N number of this JS snippets

will that code work if a server side method was called via Ajax call, for example:

$.ajax({
            type: 'GET',
            url: path + '/doTheJSStuff/',
            )};

where the doTheJSStuff is a method that echo/prints the JS code

Upvotes: 0

Views: 77

Answers (2)

Joshua Burns
Joshua Burns

Reputation: 8582

That will work, so long as the contents of the <script> tag are being passed into javascript's eval() function. If you're using a framework such as jQuery, its built-in $.ajax() method eval's tags automatically.

Upvotes: 1

Pavel Strakhov
Pavel Strakhov

Reputation: 40512

  1. Your script must return javascript code (without html tags).
  2. Call eval(text) after receiving text.

Upvotes: 3

Related Questions