7

What is the best way to send data and receive a response dependent on that data?

Consider the PHP file used for the request:

$test = $_POST['test'];

echo json_encode($test);

I have tried unsucessfully to achieve this with:

$.ajax({
    type: "POST",
    dataType: "json",
    data: '{test : worked}',
    url: 'ajax/getDude.php',
    success: function(response) {
        alert(response);
    }
});
0

3 Answers 3

8

Lose the quotes to pass the object:

$.ajax({
  type: "POST",
  dataType: "json",
  data: {test : worked},
  url: 'ajax/getDude.php',
  success: function(data) {
    alert(data);
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

TypeError: Illegal invocation
5

Instead of this

data: '{test : worked}'

try

data: {"test" : worked} // Worked being your data you want to pass..
 data: {"test" : "worked"} // Else enclose worked in quotes

Comments

1

The problem appears to be that you're submitting a string rather than a json object - change data: '{test : worked}' to data: {test : 'worked'}

Comments

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.