0

Why would this not work for me ? It's an AJAX POST request to a php script, the script has been tested and works fine with fixed variables.

The request is as follows

 $.ajax({
type: "POST",
url: url,
data:{serial:id},
contentType:"application/json; charset=utf-8",
dataType: "json",
    success: function(data){}

My php is

    <?php
header("Access-Control-Allow-Origin: *");
header('Content-type: application/json');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo 'POSTed'; 
 } //to test post

$account = $_POST['serial'];

....etc

which is followed my a MySQL request.

And from my console everything looks fine, except I get " Undefined index: serial" as my response. I have tried constructing my POST variables many different ways. It come from source which when viewed shows the correct variable.

var id =  localStorage.userAccount;
12
  • 1
    what value you see in firebug console while posting the data. Check the POST tab and let me know what is displays for the ajax request. you might be missing something there Commented Mar 5, 2013 at 9:58
  • why are you setting contentType as application/json ? Commented Mar 5, 2013 at 9:59
  • 1
    or why don't you var_dump($_POST) and check what is received. Commented Mar 5, 2013 at 9:59
  • 1
    Add an alert() or console.log before the ajax call to see the value of serial Commented Mar 5, 2013 at 9:59
  • what it prints on server if you write <?print_r($_POST);?> Commented Mar 5, 2013 at 10:00

1 Answer 1

4

no need to add contentType:"application/json; charset=utf-8", in jquery.ajax. just remove it and then try.

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

3 Comments

I actually have a json encoded response in the full script, the ('Content-type: application/json'); is actually just something I had in there from experimenting with PhoneGap, sorry about that.
That was it, never would have though of removing it as it worked with other requests, appreciate the help, Thanks
I got problem while posting my custom parameters in data part and using following ajax parameters processData: false, contentType: false SO I removed that 2 parameters and I got POST data!!! For knowledge only. Thanks!

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.