2

I have an array of tags and I want to test them on Instagram to get media_count. Then I want to send the data I get to my symfony controller. This code is working and I reach the alert in my Success function.

for (var i = 0; i < 1; i++) {
    var tagname = tags[i];      
    var url = "https://api.instagram.com/v1/tags/" + tagname + "?client_id=" + clientID;
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: false,
        url: url,
        success: function (res) { 
            var data = "{'name':'" + res.data.name + "','count':'" + res.data.media_count + "'}";
            $.ajax({  
                type: "POST",  
                url: controller-url, 
                data: data,
                success: function(response) {
                    alert(data);
                    
                }
            });             
        }
    });     
}   

Then I use the solution in this answer to decode my data in controller, like this:

public function createAction(Request $request) {
    $params = array();
    $content = $this->get("request")->getContent();
    $params = json_decode($content, true); // 2nd param to get as array
    ...
} 

But when I try to send $params to a template, it is empty. Why is that, and what am I doing wrong?

6
  • 1
    could you post what do you get in the success data? Commented Sep 30, 2013 at 9:54
  • 1
    I get my data, like this:{'name':'foo','count':'350'} Commented Sep 30, 2013 at 10:03
  • 1
    ever debuged $params? Commented Sep 30, 2013 at 10:16
  • $params is null and json_last_error() = 4 (syntax error) Commented Sep 30, 2013 at 11:15
  • why do you pass data as JSON? you could just set the name and count variables. Commented Oct 1, 2013 at 13:17

2 Answers 2

2

2 years late, but I had the same problem today and found this unanswered question :

In your callback function, assuming your data is correctly stringified (it looks like so), you forgot to specify the contentType:"application/json"

success: function (res) {
    var data = "{'name':'" + res.data.name + "','count':'" + res.data.media_count + "'}";
        $.ajax({  
            type: "POST",
            contentType : 'application/json',
            url: controller-url, 
            data: data,
            success: function(response) {
                alert(data);

            }

Otherwise, in your Symfony Controller, if you return :

$req->headers->get("Content-Type")

...you'll see that it's been sent with the default x-www-form-urlencoded

Hope it'll help someone in the future.

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

Comments

2

I looked this:

public function createAction(Request $request) {
    $params = array();
    $content = $this->get("request")->getContent();
    $params = json_decode($content, true); // 2nd param to get as array
   ...
}

The problem is that you don't take the $request from the function createAction.

public function createAction(Request $request) {
    $params = array();
    $content = $request->getContent();
    if (!empty($content)) {
        $params = json_decode($content, true);
    }
   ...
}

Now you'll get the JSON content from $request.

Cheers!

1 Comment

Nice catch! Good job

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.