robert
robert

Reputation: 17

post not working

this is my jscript code: when i run it i get no response and firefox webconsole is showing ok

dataS = friendMediaArray; // array?
var jsonS = JSON.stringify(dataS);
$(function() { 
   $("#xbut").click(function() { 
      var data = {}; 
      data.dataArray = friendMediaArray; // I'm setting it as a property here 

      var jsonS = JSON.stringify(data); 
      jQuery.ajax({ 
          type: "GET", 
          data: {type: "stream", namef: jsonS }, 
          url: 'catch.php', 



        success: function (msg) 
                { alert("OK");alert(data); console.log(data); },
        error: function (err)
        { alert(err.responseText)}
    });}); }); 

catch.php

<?php 
if($_GET['type']=='stream'){ 
     $obj = json_decode($_GET['namef']); 
   for($i=0;$obj[$i];$i++){ 
    echo $obj[$i]->{'namef'}." "; 
    } 
} 

json_decode($_GET['namef']); 

?> 

i get undefined even with var_dump($_POST) it seems like ther's nothing send

GRT robert

Upvotes: 0

Views: 94

Answers (2)

Bergi
Bergi

Reputation: 664307

dataString = friendMediaArray; // array?

If your data is an array, you really should not name it something with "string"!

var jsonString = JSON.stringify(dataString);

Now, your jsonString is a real string, and

jsonString[field.name] = field.value; 

will result in nothing. You can't set properties of primitive string values. Instead, you will need to set them as properties of object, which you then can serialize:

$(function() {
   $("#xbut").click(function() {
      var data = {};
      data.dataArray = friendMediaArray; // I'm setting it as a property here
      $.each($(':input').serializeArray(), function(i,field){
           data[field.name] = field.value; 
      });
      var jsonString = JSON.stringify(data);
      jQuery.ajax({
          type: "GET",
          data: {type: "stream", namef: jsonString },
          ...
      });
   });
});

Also note that when you send your data with GET as url parameters, $_POST will obviously be empty. Yet, json_decode($_GET['namef']); should give you the object you wanted.

Upvotes: 1

williamvicary
williamvicary

Reputation: 805

You are using type: "GET", try:

var_dump($_GET);

Upvotes: 1

Related Questions