No data response when sending ajax request to php


Markop

I want to send a json data to my php but when i access it to my php there is no response.

this is my ajax request

    var project = {project:"A"};

 var dataPost = JSON.stringify(project);

     $.ajax({
     url: 'fetchDate.php',
     data: {myData: dataPost},
     type: 'POST',
     datatype:'json',
     contentType: "application/json",  
     success: function(data) {
         alert(JSON.stringify(data));
     }
 });

});

Here is my php where I handle the request and return the data for testing

<?php header("Content-Type: application/json; charset=UTF-8");
  $objProject = json_decode($_GET["myData"]);
  echo json_encode($objProject->project); ?>

I am new to ajax so please i need your help

Meraki

You don't need to add a content-type on ajax because you're not actually sending the json to the server.

     $.ajax({
     url: 'fetchDate.php',
     data: {myData: project},
     type: 'POST',
     datatype:'json',
    // contentType: "application/json",  
     success: function(data, status, jqXHR) {

         console.log(data,status,jqXHR);
          alert(JSON.stringify(data));
     }
});

No need to stringify item object, in your php just encode it as json

<?php  header("Content-Type: application/json; charset=UTF-8");
 $obj = $_POST['myData'];
 echo json_encode($obj); ?>

you should get json string on alert

Related


No data response when sending ajax request to php

Markop I want to send a json data to my php but when i access it to my php there is no response. this is my ajax request var project = {project:"A"}; var dataPost = JSON.stringify(project); $.ajax({ url: 'fetchDate.php', data: {myData: da

PHP returns no data when sending ajax request

beans, lentils When I send an ajax post request to my getMessages.php file it returns nothing. I've tried manually setting the array values and printing them in the console and that seems to work. get message.php <?php require_once "mysqli.php"; $data = arra

PHP returns no data when sending ajax request

beans, lentils When I send an ajax post request to my getMessages.php file it returns nothing. I've tried manually setting the array values and printing them in the console and that seems to work. get message.php <?php require_once "mysqli.php"; $data = arra

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I've enabled rewrite rules in the .htaccess file in the root directory to keep pointing everyt

PHP cannot read $_GET parameter when sending AJAX request

Ed91gg I've been struggling with this since a few months and finally got to the point where I need to fix it in my project. The problem is, I am sending an ajax request to a php file where I need to read the current $_GET['user_id'] value to insert the query.

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

Ajax not sending request to php when url directory changes

Zuckerberg Now, this query is really not being asked and I ended up dropping the service because I couldn't find a sensible answer to my query. On my apache2 server, I have enabled rewrite rules in the .htaccess file in the root directory to always keep everyt

PHP cannot read $_GET parameter when sending AJAX request

Ed91gg I've been struggling with this since a few months and finally got to the point where I need to fix it in my project. The problem is, I am sending an ajax request to a php file where I need to read the current $_GET['user_id'] value to insert the query.

400 Bad Request when sending data using Ajax in Spring Framework

MJ82: Trying to pass JavaScript object to Spring MVC. I get a 400 Bad Request error. Tried all different ways but still no luck. I've attached the Controller, the objects I'm passing in JS and Java and the response. I will really appreciate. When I change the

400 Bad Request when sending data using Ajax in Spring Framework

MJ82: Trying to pass JavaScript object to Spring MVC. I get a 400 Bad Request error. Tried all different ways but still no luck. I've attached the Controller, the objects I'm passing in JS and Java and the response. I will really appreciate. When I change the

400 Bad Request when sending data using Ajax in Spring Framework

MJ82 Trying to pass JavaScript object to Spring MVC. I get a 400 Bad Request error. Tried all different ways but still no luck. I've attached the Controller, the objects I'm passing in JS and Java and the response. I will really appreciate. When I change the @

400 Bad Request when sending data using Ajax in Spring Framework

MJ82 Trying to pass JavaScript object to Spring MVC. I get a 400 Bad Request error. Tried all different ways but still no luck. I've attached the Controller, the objects I'm passing in JS and Java and the response. I will really appreciate. When I change the @

Sending ajax get request to node, response is undefined

Randy I have a node server running a single web page. The page has a contact form with a send button. If I click send, the following code runs: $('.contactsend').click(function(){ $.ajax({ method: "GET", url: "http://127.0.0.1:3000/contact"

Not receiving data in php codeigniter when sending ajax post

Allanz I'm trying to send data by post using ajax (with codeigniter), I don't know why, but I'm not getting anything... This is how I send it: var sendData = $('#formContact').serialize(); $.ajax({ type: 'POST', url: '<?php echo base_url()?>/intranet/u

Not receiving data in php codeigniter when sending ajax post

Allanz I'm trying to send data by post using ajax (with codeigniter), I don't know why, but I'm not getting anything... This is how I send it: var sendData = $('#formContact').serialize(); $.ajax({ type: 'POST', url: '<?php echo base_url()?>/intranet/u

AJAX not sending form data to PHP

Jerry I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL. $(document).ready(function(){ $('#my_form').submit(function(){

AJAX not sending form data to PHP

Jerry I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL. $(document).ready(function(){ $('#my_form').submit(function(){

jQuery Ajax not sending data to PHP

user 882670 The following myscript.js output: [{"orcamento":"10","atual":"20","desvio":"","data":"2015-01-01","nome_conta":"BBB","nome_categoria":"abc","nome_entidade":"def"}] myscript.js: if (addList.length) { $.ajax($.extend({}, ajaxObj, {

AJAX not sending form data to PHP

Jerry I am sending some form data to PHP via AJAX. Before sending the data using AJAX, the value of the input field is displayed, but in the PHP script, the value received is NULL. $(document).ready(function(){ $('#my_form').submit(function(){

jQuery Ajax not sending data to PHP

user 882670 The following myscript.js output: [{"orcamento":"10","atual":"20","desvio":"","data":"2015-01-01","nome_conta":"BBB","nome_categoria":"abc","nome_entidade":"def"}] myscript.js: if (addList.length) { $.ajax($.extend({}, ajaxObj, {

"ajaxFunction is not defined" when sending AJAX request

Chirag Senjaliya I want to update another page userreg.phpfrom index.php. Here is my code: <html> <head> <title> User List </title> <link rel="stylesheet" href="css/datacss.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs