0

I am passing values from PHP Script to JS using JSON encode.

$arr=array('X'=>$X,'Y'=>$Y,'par'=>$par);
echo json_encode($arr);

Which produces

{"X": ["-0.9537121038646844","-0.9537121038646844","-0.9537121038646844","0.9537121038646843",""],
 "Y": ["-0.9537121038646844","-0.7799936811949519","-0.5533396521383813","-0.37962122946864896",null],
 "par": ["0.009811016749950838","0.005007306592216437","5.058030686503405E-4","9.451402320405391E-4",null]}

In the javascript, I used the following command

{
   $.post("plot.php",param,function(data){dataType:"json";console.log(data);Var X1=data.X;});
}

But I am not able to obtain the values of X alone. I tried few suggestions from similar threads, but none of them did the trick.I need the three arrays, X,Y and Par to be used in JS.

2 Answers 2

1

Try this:

$.post("plot.php", param, function(data) {
    console.log(data);
    var X1 = data.X;
  },
  "json"
);

You can also use $.ajax (which is the function $.post internally calls):

{
  $.ajax({
    type: "POST",
    url: "plot.php",
    dataType: "json",
    data: param,
    success: function(data) {
      console.log(data);
      var X1=data.X;
    }
  });
}
Sign up to request clarification or add additional context in comments.

5 Comments

indeed, your datatype is in your success fucntion for some reeason
When I tried your suggestion, my console.log(X1) doesnt print the X1. I am using firebug. Any insight on what could be happening ? Thanks.
Try console.log(data), do you get any output?
I do get data on the console. I have pasted the data as a part of the question. Just that am not able to separate the data into X,Y and par individually.
@SaiSudharsanan In which format do you get the data in the console? Is it a string or a JSON object? If it is a string, which MIME type does your server serve for your URI?
1

If you are using php you have to specify at the last parameter of post fn that is json type:

$.post("plot.php",param, function(data){ 
 console.log(data);
 var X1=data.X;
},'json');

1 Comment

Thank you for your reply. I had tried this. Yet I am not sure if this works. My console.log(X1) doesnt even print X1.I am using firefox browser and firebug. Thanks, Sai

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.