When i'm trying to send data(url) to API with ajax request, PHP CI controller not considering all parameters in $_GET. it just accepting up to &.
Is there any error in passing parameters? Please help. I tried passing direct url and encodeURI.
JQuery code
$.ajax({
type: "GET",
url: "<?= ROOT?>welcome/ajaxapiget",
data: encodeURI(http://localhost/webapi/list?categories[]=3&categories[]=12),
cache: false,
success: function (data) {
alert(data);
}
});
PHP
function ajaxapiget() {
$url = $_GET;`//its getting url as http://localhost/webapi/list?categories[]=3`
$curl = curl_init();
........
........
response $response;
}
also tried like this
$.ajax({
type: "GET",
url: "<?= ROOT?>welcome/ajaxapiget",
data: 'url=http://localhost/webapi/list?categories[]=3&categories[]=12)',
cache: false,
success: function (data) {
alert(data);
}
});
PHP
function ajaxapiget() {
$url = $_GET(url);`//its getting url as http://localhost/webapi/list?categories[]=3`
$curl = curl_init();
........
........
response $response;
}
When i alert
before sending request
url=http://localhost/webapi/courselist?categories[]=15&categories[]=17
response from controller (alert at ajax success)
$url = $_GET('url');
echo $url
alert response
http://localhost/webapi/courselist?categories[]=15
Try this
JQuery code
$.ajax({
type: "GET",
url: "<?= ROOT?>welcome/ajaxapiget",
data: {url:'http://localhost/webapi/list?categories[]=3&categories[]=12'},
cache: false,
success: function (data) {
alert(data);
}
});
PHP code
function ajaxapiget() {
$url = $this->input->get('url');//Getting $url as http://localhost/webapi/list?categories[]=3&categories[]=12
$curl = curl_init();
........
........
response $response;
}
Use $this->input->get() instead of $_GET[] in Codeingiter
Finally i solved this with encodeURIComponent
$.ajax({
type: "GET",
url: "<?= ROOT?>welcome/ajaxapiget",
data: 'url=http://localhost/webapi/list?' + encodeURIComponent('categories[]=15&categories[]=17'),
cache: false,
success: function (data) {
alert(data);
}
});
now the result is
http://localhost/webapi/courselist?categories[]=15&categories[]=17
encodeURIComponent will encode everything with special meaning, so you
use it for components of URIs
Source : https://stackoverflow.com/a/4540785
Related
When i pass number with function argument for sending using ajax its working properly but when i pass string such as given below its not receiving in php page.
function loadState(country) {
$.ajax({
url: 'cardstate.php',
type: 'get',
data: { 'country': country },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(data) {
alert(data);
}
});
}
loadState("Pakistan")
// cardstate.php:
$country = $_GET['country'];
echo $country; // not receving string while when i put number its receiving
In your cardstate.php you should use json_encode
// cardstate.php:
$country = $_GET['country'];
echo json_encode($country); // not receving string while when i put number its receiving
function loadState(country) {
$.ajax({
url: 'cardstate.php',
type: 'GET',
data: 'country='+country, // <--- changed this as its GET
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function(data) {
alert(data.country); // <---- made this change
}
});
}
loadState("Pakistan")
And in your PHP, use json_encode()
// cardstate.php:
$country = $_GET['country'];
echo json_encode(array('country' => $country));
I have an ajax post that looks like this: (Located in: post.php)
$.ajax({
type: "POST",
url: 'prize.php',
cache: false,
beforeSend: function(req) {
req.setRequestHeader("Accept", 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
},
data: {sw: screen.width, sh: screen.height, saw:screen.availWidth, sah: screen.availHeight, scd: screen.colorDepth, tz: (new Date().getTimezoneOffset()), bp: sbp, hf: have_flash},
success: function (data, textStatus, xhr) {
if(data=="success"){
$('#status').text("You won: $<?php echo $data['prize'] ?>!");
}else {
$("#m_error_msg").html(data);
}
},error: function (){
}
});
The above ajax call, posts to this page: prize.php That looks like this:
if($_POST){
$data = array("data"=>"success","code"=>"100","prize"=>"$prize","type"=>"$text");
die($data['data']);
}
My question is.. How can I pass the $data['prize'] or $data['type'] to the:
if(data=="success"){}
code?
Add dataType:'json' to your $.ajax() handler to declare you wish to receive a json encoded result back from the server:
type: "POST",
url: 'prize.php',
cache: false,
dataType:'json',
Then in your response from the server, send back a json_encodeded array.
echo json_encode($data);
die();
Then in your success function, let's check:
success: function(data){
if(data.data == 'success'){
}
}
My jQuery ajax posting is not working. Here is the javascript
function SocialButtons() {
var $buttonWrapper = jQuery('.WrapperDiv');
if ($buttonWrapper.length){
var postData = $buttonWrapper.html();
jQuery.ajax({
type: 'POST',
url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
data: postData,
cache: false,
success: function(data) {
console.log(data);
},
contentType: "application/json",
dataType: 'json'
});
}
}
I am saving the data to be posted inside a hidden div like
<div class='WrapperDiv hidden'>{"post_id":392,"url":"http:\/\/www.wordpress-site\/post\/post-title\/","title":"SEO Friendly title"}</div>
All I am getting in return from the post.php page is an empty array. Here is my code for post.php
<?php
if(isset($_POST)){
print_r($_POST);
} else {
echo "0";
}
?>
Any Idea whats wrong?
EDIT : Its working after I removed
contentType: "application/json",
dataType: 'json'
What about something like this:
var postData = "data=" + encodeURCIComponent($buttonWrapper.html());
Than in PHP:
echo $_POST["data"];
Than parse it or something....
Couple of things to try,
Try to pass the data directly in to the data object first. If it
works then you can debug and see why it's not ready your hidden div.
instead of $buttonWrapper.html try $buttonWrapper.text();
function SocialButtons() {
var $buttonWrapper = jQuery('.WrapperDiv');
if ($buttonWrapper.length){
var postData = $buttonWrapper.**text**();
jQuery.ajax({
type: 'POST',
url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
data: **{'id':1}**,
cache: false,
success: function(data) {
console.log(data);
},
contentType: "application/json",
dataType: 'json'
});
}
}
Inside your jQuery ajax call, your data is not set to $_POST variable names. Hence why nothing is showing
Try changing your function to this:
function SocialButtons() {
var buttonWrapper = jQuery('.WrapperDiv');
if (buttonWrapper.length){
var postData = buttonWrapper.html();
jQuery.ajax({
type: 'POST',
url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
data: {postData: postData},
cache: false,
success: function(data) {
console.log(data);
},
contentType: "application/json",
dataType: 'json'
});
}
}
Then you should have a $_POST['postData'] variable on your print_r or var_dump of $_POST.
Its working after I removed
contentType: "application/json",
dataType: 'json'
I'm using jQuery and jquery-json to post data to a PHP script.
$.post('<?php echo url_for('/ajax.php'); ?>', 'data=' + $.toJSON(order), function (response) {
if (response == "success") {
$("#respond").html('<div class="success">Item Saved!</div>').hide().fadeIn(1000);
setTimeout(function () {
$('#respond').fadeOut(1000);
}, 2000);
}
})
If I console.log(order) I get the following JOSN:
{"details":[{"template_id":"25","font_size":"22"}]}
In my ajax.php file I have:
$data = json_decode($_POST["data"]);
var_dump($data);exit;
Which returns 'NULL'
But when I have the following code:
$data = $_POST["data"];
var_dump($data);exit;
It returns:
string(61) "{\"details\":[{\"template_id\":\"25\",\"font_size\":\"26\"}]}"
Is there any reason why it is escaped?
What is the easiest way to decode this?
Thanks
you may need to disable magic_quotes_gpc in your php.ini or .htaccess file which is adding the slashes to your post variables.
Or you could just call stripslashes on $_POST['data'] like so:
$data = json_decode(stripslashes($_POST["data"]));
You need to add dataType: 'json' to your ajax call.
$.ajax({
url: url,
type: 'post',
dataType: 'json',
data: $.toJSON(order),
async: true,
success: function (data) {
if (data.response) {
$("#respond").html('<div class="success">Item Saved </div>').hide().fadeIn(1000);
setTimeout(function () {
$('#respond').fadeOut(1000);
}, 2000);
}
}
});
How can I recreate a request with jquery which would work exactly as this PHP request?
$client = new Zend_Http_Client($uri);
$response = $client->setMethod(Zend_Http_Client::POST)
->setRawData(trim($json), 'application/json')
->request();
Thanks.
$.ajax({
cache:false,
type: 'POST',
url: "yoururl",
data:yourJsonData,
contentType: "application/json",
success: function(data) {
//do what you what with return data
}
});
Like this
$.postJSON("/application/json".
{
ID:5 // all parameters here for the method
},
function(data)
{
//data is what comes back from your function
});