0
var settings = {
  "async": true,
  "crossDomain": true,
  "url": 'https://example.com/something.aspx?i='<? echo urlencode($_GET['id']); ?>,
  "method": "GET",
  "headers": {
    "cache-control": "no-cache",
  }
}

It doesn't work this way, the concatenation is wrong I think. Tried few ways still doesn't work.

3
  • "crossDomain": true, — This almost certainly doesn't do what you think it does. Look it up and decide if you really need it. Commented Jun 26, 2016 at 16:09
  • If you looked at what the browser received, surely it would have been fairly clear what was wrong. Commented Jun 26, 2016 at 16:09
  • 'https://example.com/something.aspx?i=' + <? echo urlencode($_GET['id']); ?> Commented Jun 26, 2016 at 16:22

3 Answers 3

5

You need to put the data inside the JavaScript string literal. Move the ' to after the extra data you are outputting.

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

Comments

3

You just had the single quote the wrong side.

Don't forget you're outputting to HTML, so you don't have to concatenate a PHP variable to a JavaScript variable.

var settings = {
  "async": true,
  "crossDomain": true,
  "url": 'https://example.com/something.aspx?i=<?php echo urlencode($_GET['id']); ?>',
  "method": "GET",
  "headers": {
    "cache-control": "no-cache",
  }
}

Comments

-1

Maybe you can try it the other way round. Like

<?php
  echo 'var settings = {
    "async": true,
    "crossDomain": true,
    "url": "https://example.com/something.aspx?i='.urlencode($_GET['id']).'",
    "method": "GET",
    "headers": {
      "cache-control": "no-cache",
    }
  }'; 
?>

Please note the changes I made with the ' and " in the url attribute.

1 Comment

You're still closing the " .. " string here, so this won't work.

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.