-1

I have a JSON with an invalid space-character in it. I want to remove this space before I send it via Ajax (POST). Because of the space after "StopsCompleted" that can not be realise in PHP (empty response).

excerpt from JSON

{"Name":"StopsCompleted ","Category":"Economy","DisplayName":"Haltestellen angefahren ","DisplayDetails":"2/2","Points":10,"IsBoolean":"false",

file.php

jQuery.getJSON("/SourceFile/Vehicles", function(data){
                                
    jQuery.ajax({
        url: '/SourceFile/Vehicles_ajax.php', 
        type: 'POST',
        data: JSON.stringify(data),
        processData: false,
        contentType: "application/json; charset=UTF-8",
        success: function(data){
            jQuery('.ausgabe').html(data);
        }
    });

});

Vehicles-ajax.php

$jsonString = file_get_contents('php://input');
$json = json_decode($jsonString);
print_r($json);

Anyone ideas for the problem?

Thank you very much.

Denis
  • 51
  • 1
  • 1
  • 6
  • 1
    Space has nothing to fo with your problem. Use `json_last_error()` and get full error description. – u_mulder Mar 29 '21 at 19:57
  • 1
    Also think that this is an imaginary problem, as space barely is an illegal character. – Martin Zeitler Mar 29 '21 at 20:05
  • The string is no good. There is a trailing `,` at the end. It needs to be a `}` in order to close it out properly. The space after `StopsCompleted` has nothing to do with it because the space is part of the string. – dazed-and-confused Mar 29 '21 at 20:06
  • Just see what the JavaScript does ...there is no function intended. – Martin Zeitler Mar 29 '21 at 20:08
  • With fail I can see this error: `SyntaxError: Unexpected token in JSON at position 1783` which is at the place where "StopsCompleted " is. – Denis Mar 30 '21 at 05:24
  • Apply `encodeURIComponent` to the value of that data element containing `StopsCompleted ` and log it to console - the percent encoding will help you determine _what_ character you actually got in that position. – CBroe Mar 30 '21 at 07:22
  • @CBroe It's > 09 <; see `Name%22%3A%22StopsCompleted%09%22%2C%22Category` – Denis Mar 30 '21 at 11:38
  • That would be a standard horizontal tab character then. Not sure why you would not get that as `\t` to begin with though, because that should be the default behavior of JSON.stringfy. Result of `JSON.stringify("foo"+String.fromCharCode(9)+"bar")` _is_ `"foo\tbar"` – CBroe Mar 30 '21 at 11:44

1 Answers1

0

Now I have solved it, but I have a string which must converted to an array.

var data;
                        
                        jQuery(document).ready(function ($) {
                            
                                                        
                            $('#los').click(function() {
                                    
                                fetch('/SourceFileJSON/Vehicles')
                                    .then(function (response) {
                                        return response.text();
                                      
                                    })
                                    .then(function (data) {
                                        
                                        data = String(data).replace("StopsCompleted ", "StopsCompleted");
                                        
                                        $.ajax({
                                    
                                            url: '/SourceFileJSON/Vehicles_ajax.php', 
                                            type: 'POST',
                                            data: JSON.stringify(data),
                                            contentType: "application/json; charset=UTF-8",
                                            success: function(data){
                                                $('.ausgabe').html(data);
                                            },
                                            error: function(data){
                                                console.log(arguments);
                                            }
                                            
                                        });
                                        
                                    });
                                        
                            });

ajax-file:

$jsonString = file_get_contents('php://input');
$json = html_entity_decode($jsonString);
$json = json_decode($json, true);
var_dump($json);
Denis
  • 51
  • 1
  • 1
  • 6