不能让jQuery的AJAX POST工作 [英] Cant get jQuery ajax POST to work

查看:78
本文介绍了不能让jQuery的AJAX POST工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将类型设置为GET在code以下它的工作原理,但我不能把它与POST工作。

If I set type to 'GET' in the code below it works, but I cant get it to work with 'POST'.

ajaxPostTest.html ...

ajaxPostTest.html...

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    $.ajax({
        url: "ajaxPostTest_server.php",
        data: {"fruit1": "rasp", "fruit2": "bramble"},
        type: 'POST',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(data){
            $("#returned").append(data.fruit1);
            },
        error: function(jqXHR, textStatus, errorThrown){
             alert("error") ;
            }
        });
});

</script>
</head>
<body>
<div id="returned">returned: </div>
</body>
</html>

ajaxPostTest_server.php ...

ajaxPostTest_server.php...

<?php
    echo json_encode($_REQUEST);
?>

在浏览器中的预期成果是...

The expected output on the browser is...

returned: rasp

实际上,我尝试用ASP.NET Web服务器交互,我想证实我的AJAX是工作(我知道阿贾克斯正确)第 - ,因此这种简化的code

I'm actually attempting to interact with an ASP.NET web server and I want to confirm my ajax is working (and I understand ajax properly) first - hence this simplified code.

Firebug的调试...

Firebug debugging...

Response Headersview source
Date    Fri, 23 Sep 2011 14:57:37 GMT
Server  Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By    PHP/5.3.1
Keep-Alive  timeout=5, max=99
Connection  Keep-Alive
Transfer-Encoding   chunked
Content-Type    text/html
Request Headersview source
Host    localhost
User-Agent  Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18
Accept  application/json, text/javascript, */*; q=0.01
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Content-Type    application/json; charset=utf-8
X-Requested-With    XMLHttpRequest
Referer http://localhost/My_Webs/temp/ajaxPostText1.html
Content-Length  26
Cookie  PHPSESSID=mgvoacnluh3kad5pakafrd5kd1

我得到的响应选项卡...

All I get in the 'response' tab is...

{"PHPSESSID":"mgvoacnluh3kad5pakafrd5kd1"}

即。我找不到,我发送到服务器的数据将要。

I.e. I cant find where the data I send to the server is going to.

推荐答案

由于您的发送数据为应用程序/ JSON,PHP不填充$ _ POST / $ _REQUEST。您需要发送请求为应用程序/ x-WWW的形式urlen codeD(你可以离开了内容类型参数,因为这是默认的类型)。

Because your sending your data as "application/json", PHP does not populate $_POST / $_REQUEST. You need to send the request as "application/x-www-form-urlencoded" (you can leave out the "Content-Type" parameter because this is the default type).

这篇关于不能让jQuery的AJAX POST工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆