php - AJAX value sending error -
hi have problems script below. problem think lies on data need sent php via ajax.
jquery
$('.send').live("click", function(){ $.ajax({ url:'foobar.php', type:'post', data: 'id=' + $(this).attr('id'), datatype:'json', contenttype: 'application/json; charset=utf-8', success: function(data) { switch (data.status) { case "a": alert(data.text); break; case "b": alert(data.text); break; } }, error: function(xmlhttprequest, textstatus, errorthrown) { alert ("error: "+textstatus); } }) }
and, php
$id = $_request['id']; switch ($id) { case "foo": $data["status"] = "a"; $data["text"] = "foo-foo"; echo json_encode($data); break; case "bar": $data["status"] = "b"; $data["text"] = "bar-bar"; echo json_encode($data); break; }
but, if this
//data: 'id=' + $(this).attr('id'),
and change this
$id = 'foo';
the script work fine. need make both script above can work? in advance.
i put comment answer.
apart using deprecated jquery api, others didn't point out following:
what doing in below line:
contenttype: 'application/json; charset=utf-8',
is promising server http entity json-string, not case. usual percentile-encoded string. (a=b&c=d&e=f
).
if remove line, browser sends default value content-type application/x-www-url-form-encoded
. trigger php parse http entity such , give $_request
array populated.
Comments
Post a Comment