[英]Is this a jQuery bug? $.post vs $.ajax
UPDATE: my code may be confusing and contain errors.更新:我的代码可能令人困惑并包含错误。 But here is what I'm trying to do with both $.ajax and $.post ----I want to send JSON to php and receive plain text.
但这是我试图对 $.ajax 和 $.post 做的事情----我想将 JSON 发送到 php 并接收纯文本。 But $_POST is empty<-why?-----
但是 $_POST 是空的<-为什么?-----
I found the $_POST to be empty if I use $.ajax, but contains the right content when using $.post Update: Requirement-- I need to use a JSON request.如果我使用 $.ajax,我发现 $_POST 为空,但在使用 $.post 更新时包含正确的内容:要求——我需要使用 JSON 请求。 But problem is $_POST is empty in php, see Var_dump This is my script
但问题是 $_POST 在 php 中为空,请参阅 Var_dump 这是我的脚本
<script>
$(document).ready(function(){
$("#submit").click(function(){
$.ajax({
type: "POST",
url: "Ajax_example.php",
data: {"field": $("#field").val()},
contentType: "application/json; charset=utf-8",
success: function(msg){
alert("NEW DATA:"+msg);
}
});
/* $.post("Ajax_example.php", {"field": $("#field").val()},
function(msg){
alert("NEW DATA:"+msg);
});*/
return false;
})
});
</script>
This is my php这是我的 php
<?php
echo var_dump($_POST);
?>
With the above, $.post portion are comments.以上,$.post 部分是评论。 With the $.ajax portion running, it alerts out only "NEW DATA:" But If I do it the other way around, I will get whatever I put in #field
随着 $.ajax 部分运行,它只提示“新数据:”但如果我反过来做,我会得到我在#field 中输入的任何内容
You're using $.ajax()
incorrectly.您错误地使用
$.ajax()
。 The data
parameter to $.ajax
is wrapped in a string. $.ajax
的data
参数被包裹在一个字符串中。 Try this:尝试这个:
$.ajax({
type: "POST",
url: "Ajax_example.php",
data: {field: $("#field").val() },
success: function(msg){
alert("NEW DATA:"+msg);
}
});
In the section which you say is not working, you're going through great lengths to make a string for your data, but in the section you say is working you are passing an object literal (preferred).在您说不工作的部分中,您正在竭尽全力为您的数据创建一个字符串,但是在您说正在工作的部分中,您正在传递一个 object 文字(首选)。 Fix the $.ajax section to pass an object literal as the
data
option.修复 $.ajax 部分以传递 object 文字作为
data
选项。
You're also passing a contentType
option, but no dataType
option.您还传递了一个
contentType
选项,但没有传递dataType
选项。 Use either of the versions in the code block below:使用以下代码块中的任一版本:
$( function()
{
$( '#submit' ).click( function( e )
{
/**
THIS
*/
$.ajax( {
type: 'POST',
url: 'Ajax_example.php',
data: {
field: $( '#field' ).val()
}
success: function( msg )
{
alert( 'NEW DATA:' + msg );
},
dataType: 'json'
} );
/**
IS THE SAME AS THIS
*/
$.post( // type implied in method name
// url
'Ajax_example.php',
// data
{
field: $( '#field' ).val()
},
// success
function( msg )
{
alert( 'NEW DATA:' + msg );
},
// dataType
'json'
);
/**
DON'T RETURN FALSE...CONTROL YOUR EVENT INSTEAD
*/
e.preventDefault();
} );
} );
Your data is in $receive variable, not $_POST.您的数据在 $receive 变量中,而不是 $_POST。 Try:
尝试:
<?php
$receive=file_get_contents('php://input');
$receive=json_decode($receive, TRUE);
echo $receive['field'];
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.