Reputation: 421
I am trying to get variables from an external domain using ajax and then create pre-filled form fields with the data, but i cant seem to get it to work.
I am a bit of a noob to JavaScript it might be something stupid but i just can't figure out why it is not working.
<html>
<head>
<script type="text/javascript">
function xss_ajax(url) {
var script_id = null;
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
script.setAttribute('id', 'script_id');
script_id = document.getElementById('script_id');
if(script_id){
document.getElementsByTagName('head')[0].removeChild(script_id);
}
// Insert <script> into DOM
document.getElementsByTagName('head')[0].appendChild(script);
}
function callback(data) {
var counter = 1;
var limit = 3;
var txt = '';
for(var key in data) {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]' value=" + data[key] +">";
document.getElementById(divName).appendChild(newdiv);
counter++;
;
}
}
var url = "http://myserver.com/test.php";
</script>
<title></title>
</head>
<body>
<button onclick="xss_ajax(url);">Get Data</button>
</body>
</html>
The script on the external domain.
callback({"firstname":"John", "lastname":"Smith", "email":"[email protected]"});
Upvotes: 0
Views: 362
Reputation: 1037
You need to look into JSONP. Also using a library to handle the request will help exponentially, checkout jQuery or Extjs. Heres a blog post that should give you a hand.
Upvotes: 1