skunk a
skunk a

Reputation: 245

call a $.getJSON and sending URL parameter

I have a page with the following URL test.php?city=Paris and I have a php script (getData.php) which executes a SQL request and return a JSON object. To execute my request I need the parameter city in my URL. I call the getData.php script like this :

var ville = "<?php echo $_GET['ville']?>";
$.getJSON("bat/getData.php", {ville: ville}, function( data ) {
    console.log(data);
});

I don't think that is the best way to send the URL parameter to my php script. What do you think?

Upvotes: 0

Views: 70

Answers (2)

Brad
Brad

Reputation: 163234

You should not echo arbitrary data into a script. You have opened yourself up to cross-site scripting attacks.

You can get around the problem by JSON-encoding your data, which is compatible with JavaScript.

var ville = <?php echo json_encode($_GET['ville']); ?>;

Upvotes: 1

CodeZombie
CodeZombie

Reputation: 5377

There is nothing wrong with passing parameters as part of the query string.

But implementing a little REST service is probably more elegant. Based on your current implementation the REST service would provide the following resources:

GET /cities/{cityname}

Example:

GET /cities/paris

Upvotes: 0

Related Questions