Artur Kadziel February 2016

ajax Cannot json_decode a GET string

I am using angularjs to get userId, userTitle and userComment from form. From that controller I am sending it to php page. That page should communicate with a server. When I try to send integer all is fine, but when I try to send string I cant get echo. This is my function in controller:

$scope.addComment = function(userId, userTitle, userComment) {
           $.ajax({                           
              url: 'http://localhost/blip/app/phpCore/sendReview.php',//the script to call to get data          
              data: { 
              userId:userId,
              userTitle:userTitle,
              userComment:userComment
              },//you can insert url argumnets here to pass to review.php
              type: 'get', //for example "get"
              dataType: 'json'})//data format   
              .done(function( msg ) { //on recieve of reply
              alert( "Data Saved: " + msg );
          });  // end ajax   
         }; //end angular scope  

This is my PHP:

<?php
include('blip_4815162342_108.php');
$UserID = $_GET['userId'];
echo $UserID;
?>

When I am sending integer as "userId:userId" echo $UserID; is working and returning into .done alerting "Data Saved: " + 123. Code in php is more complicated, but I stock in this point.

Answers


dan08 February 2016

In your AJAX request you are specifying that the server will return JSON.

From jQuery docs:

dataType (default: Intelligent Guess (xml, json, script, or html))

Type: String The type of data that you're expecting back from the server.

Since the server is returning a string echo $UserID; The Ajax function will actually go to its error function, which you have not defined. If you add a .error(function(e){console.log(e);}); you'll see the userId.

So just make your server return JSON by json_encoding your return values:

<?php
include('blip_4815162342_108.php');
$UserID = $_GET['userId'];
echo json_encode(['userID':$UserID]);
?>

Or change your AJAX call to accept non-JSON. Just remove the dataType declaration.


Jeremythuff February 2016

In angular you might want to use $http...

Its usage is something like this:

$scope.addComment = function(userId, userTitle, userComment) {
    var data = JSON.stringify({ 
          userId:userId,
          userTitle:userTitle,
          userComment:userComment
          });

    var req = {
       method: 'POST',
          url: 'http://example.com',
       headers: {
          'Content-Type': undefined
       },
          data: data
       }

    $http(req).then(function successCallback(response) {

        JSON.parse(response)

        // this callback will be called asynchronously
        // when the response is available
     }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
     });  
}; //end of scope

Remember you will need to inject $http into your controller. I have edited this to be a post, as it seems like that is more of the functionality that you are looking for. You would need to change your php in that case to get the data from the POST and not the GET.

Post Status

Asked in February 2016
Viewed 3,367 times
Voted 4
Answered 2 times

Search




Leave an answer