0

I want to make an ajax request that sends a variable, in my case length to another

This is the ajax request:

  var length = 5;

  $.ajax({
    method:'POST',
    data: {
      "id": id,
      "length": length,
      "start": start,
    },
    url:'{{ path('json', { 'fileName': output.fileName }) }}',
    success : function (data) {
       alert("success");
  } 

Controller:

/**
 * @Route("/_json/{fileName}", name="json", methods={"GET","POST"})
 */
public function jsonGenerator(JsonGenerator $jsonGenerator, $fileName)
{
    $output = $jsonGenerator->getJson($fileName);

    return $output;
}

Then my class:

class jsonGenerator
{
    public function __construct(
        EntityManagerInterface $em, 
        ClassMetadataFactoryInterface $classMetadataFactory = null, 
        NameConverterInterface $nameConverter = null, 
        PropertyAccessorInterface $propertyAccessor = null, 
        PropertyTypeExtractorInterface $propertyTypeExtractor = null, 
        ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, 
        callable $objectClassResolver = null, 
        array $defaultContext = []
    ){
        $this->em = $em;
    }

    public function getJson($fileName)
    {
        if (isset($request)) {
            $length = $request->request->get('length');
        } else {
            $length = 10;
        }

        $file = 'files/'.$fileName.'.json';

        $input = file_get_contents($file);
        $array = json_decode($input);

        foreach ($array as $key => $value) {
            if ('data' == $key) {
                $new = \array_slice($value, 0, length);
            }
        }

        $array->data = $new;

        $output = json_encode($array);

        return new Response($output);
    }
}

My problem is, that my request is not going through. The length stays always 10, but I expect the output to have the length 5.

Am I missing something?

7
  • You do actually need to inject the request into the function. Your length is always 10 because if(isset($request)){ is always not set. Your function should look more like this public function jsonGenerator($fileName, Request $request, JsonGenerator $jsonGenerator) { and ofc you need to pass on the $request to the called function down the line. Commented Feb 28, 2020 at 13:25
  • @Andrei I changed it as you recommended to public function jsonGenerator(JsonGenerator $jsonGenerator, Request $request, $fileName) { but still the length is 10. Commented Feb 28, 2020 at 13:30
  • Did you also pass the request down the line to $jsonGenerator->getJson($fileName);? Commented Feb 28, 2020 at 13:30
  • Do you mean like this: $jsonGenerator->getJson($fileName,length);? Commented Feb 28, 2020 at 13:31
  • No. No, I don't mean that. Perhaps you should review the basics of functions and such in PHP. This seems more of a basic understanding of coding rather than an actual problem. You have a variable in X scope and you want it in Y scope of function Z. You must pass variable X to function Z via it's parameters. Commented Feb 28, 2020 at 13:32

1 Answer 1

1

in your Class and method

public function getJson($fileName)

try to do this

public function getJson($fileName, Request $request)

Sign up to request clarification or add additional context in comments.

1 Comment

The answer is public function jsonGenerator(JsonGenerator $jsonGenerator, Request $request, $fileName) { $length = $request->request->get('length'); $output = $jsonGenerator->getJson($fileName,$length);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.