I have used postman to send the json data to the specifified url.I am getting the requred response that is json in postman.I want to send this json to some other file so i used ajax post method.Ajax post method is not called.My function is not triggering success or error here.
<?php
// get the HTTP method, path and body of the request
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
$input = json_decode(file_get_contents('php://input'),true);
$value1=json_encode($input);
//echo print_r($value1,1);
?>
<script type="text/javascript">
$(document).ready(function() {
// alert("hello");
var jsondata = <?php echo $value1 ?>;
$.ajax({
url: "/restapi/subscribe.php",
type: "POST",
data: { jsondata:jsondata },
dataType: "json",
Content-Type: "application/json",
success: function() {
alert("data sent!");
},
error: function() {
alert("There was an error. Try again please!");
}
});
return false;
});
</script>
I am sending any of the json data as input.Output also json data that should be posted in subscribe.php.In subscribe.php i have written
<?php
$json=$_POST['jsondata'];
echo $json;
?>
Try with below code
<script type="text/javascript">
$(document).ready(function() {
// alert("hello");
var jsondata = <?php echo $value1 ?>;
$.ajax({
url: "/restapi/subscribe.php",
type: "POST",
data: { jsondata:jsondata },
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function() {
alert("data sent!");
},
error: function() {
alert("There was an error. Try again please!");
}
});
return false;
});
</script>
and let me know if it work
You should remove Content-Type: "application/json" from your js code and use next code in your php script:
header('Content-Type: application/json');
echo json_encode($data);
I think the problem is with Content-Type
Try to replace it with contentType:"application/json"
Try to replace Content-Type: "application/json" with contentType: "application/json; charset=utf-8",
Related
I've been trying to send this information to the js.
$statement = ibase_prepare($sql);
$query = ibase_execute($statement);
while ($row = ibase_fetch_assoc($query)) {
$data[] = $row;
}
echo json_encode($data);
However, when I try to, I get the response, but I can't get the data.
$('document').ready(function (){
$.ajax({
method: "POST",
url: "../Dashboard/php/chart.php",
dataType: "json",
sucess: function (data){
console.log(JSON.stringify(data));
}
});
})
You just have a grammatical error in a word "sucess", the correct word is "success"
$.ajax({
method: "POST",
url: "./data.php",
dataType: "json",
sucess: function (data){ // Here -> success
alert(JSON.stringify(data));
}
});
I'm trying to make a login through Facebook, in the console it shows that I'm getting the requested data (email, first_name ...) but ajax keeps sending null data to the PHP method.
here's the ajax code:
function saveUserData() {
FB.api('/me?fields=id,first_name,last_name,email', function(response){
$.ajax({
url: '<?php echo base_url();?>Auth/fb_auth',
data: {response: JSON.stringify(response)},
type: 'POST',
dataType: 'json',
success: function (data) {
if(data['state'] == 'ok'){
window.location.href ="<?php echo base_url(); ?>";
}
}
});
});
}
and this is how I get the data in the method:
$response = json_decode($this->input->post('response'), true);
You donĀ“t need to stringify stuff with $.ajax, it does that for you. Also, you can use $.post instead:
function saveUserData() {
FB.api('/me?fields=id,first_name,last_name,email', function(response){
$.post({
url: '<?php echo base_url();?>Auth/fb_auth',
data: response,
dataType: 'json',
success: function (data) {
if(data['state'] === 'ok'){
window.location.href ="<?php echo base_url(); ?>";
}
}
});
});
}
here is my code in ajax
function loadcountries()
{
var p = document.getElementById("selectCntry");
while(p.firstChild)
{
p.removeChild(p.firstChild);
}
var data = {
action: "loadccc"
};
jQuery.ajax
(
{
type: "POST",
url: "ajax-ows2.php",
dataType: 'json',
async:false,
data:data,
success: function(msg)
{
alert(msg.test);
}
}
);
}
here is my ajax-ows2.php
<?php
$action = $_POST["action"];
include "dbconnect.php";
if($action == "loadccc")
{
$var = $action;
$response_array['test'] = $var;
header('Content-type: application/json');
echo json_encode($response_array);
}
?>
and here is my function call:
<script>
window.onload = loadcountries;
</script>
my ajax way is different. I really have no idea why it is not alerting when the page is load. Im really sure that my ajax-ows2.php is good and im sure that my function call is correct. Can somebody help me with this. This is not a duplicate one. I tried to used asynch:false but still not working.
try this format:
$.ajax({
method: "POST",
contentType: "application/json; charset=utf-8",
data: data,
url: 'ajax-ows2.php',
success: function (data) {
console.log(data);
},
error: function (error){
console.log(error);
}
});
since you are doing POST method, your data parameter must be a stringify, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
I have object in javascript and I need send this object to php using ajax.
I can "just send" this object, but my problem is that I need send object value types exactly as they are and not everything as string: meaning NULL as NULL, boolean values as boolean and so on...
Trying this:
var js_object= <?php echo json_encode( array("a"=>NULL, "b"=>TRUE, "C"=>1.1) ); ?>;
$.ajax({
type: "POST",
url: "some.php",
dataType: "json",
data: JSON.stringify(js_object),
contentType: "application/json; charset=UTF-8",
success: function(msg){
}
});
but this does not sends data to server at all. and not gives any error also. where I am wrong ?
There is nothing wrong with your code.
I've just add a snippet with post query to httpbin.org:
$.ajax({
type: 'POST',
url: '//httpbin.org/post',
data: JSON.stringify({name: 'test', 'null': null, 'true': true}),
success: onSuccess,
error: onError,
dataType: 'json',
contentType: 'application/json; charset=UTF-8'
});
function onSuccess(data){
// data is parsed json response
console.log('Request copy:', data.json);
console.log('Full response', data);
}
function onError(xhr) {
console.log(xhr.status, xhr.responseText);
}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
Server side sample script:
<?php
$json = file_get_contents('php://input');
$data = json_decode($json, true);
header('Content-type: application/json');
echo json_encode($data);
I am making the following jquery ajax call to a codeigniter php function:
$.ajax({
type:"POST",
url: "Ajax/getHtml",
data: { u : 'http://stackoverflow.com/' },
contentType: "application/json; charset=utf-8",
dataType: 'html',
success: function(data) {
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('error');
console.log(jqXHR,textStatus, errorThrown);
}
});
The requested php function is :
public function getHtml() {
var_dump($_POST);
$url = $_POST['u'];
$result = file_get_contents($url);
echo ($result);
}
var_dump($_POST) yields:
array(0) { }
How can I fix this?
Php will not populate the $_POST array if the content type of the request is application/json; charset=utf-8, also you aren't sending json. Just remove the content type line and the proper(default) content type of application/x-www-form-urlencoded will be set.