Php AJAX不发布JSON数据

Php AJAX不发布JSON数据,php,jquery,json,ajax,Php,Jquery,Json,Ajax,我正在尝试使用AJAX和JSON将表单数据发布到另一个页面。这是我的AJAX代码 var myData = '{ "number1": ' + $("#text1").val() + ', "number2": ' + $("#text2").val() + ' }'; $.ajax({ url: $("form").attr("action"), type: 'POST', success: function(response){ var p = $(

我正在尝试使用
AJAX
JSON
将表单数据发布到另一个页面。这是我的AJAX代码

var myData = '{ "number1": ' + $("#text1").val() + ', "number2": ' + $("#text2").val() + ' }';

$.ajax({
    url: $("form").attr("action"),
    type: 'POST',
    success: function(response){
        var p = $("p");
        p.append(response);
        //console.log(response);
    },
    error: function(request,error,msg){
        $("p").html(error+": "+msg);
    },
    data: myData,
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
});

我正在使用目标页面上的
print\r
功能打印
$\u POST
数组,以查看是否收到任何参数。但我发现它是空的。获取
parserror:SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符。

change
var myData='{“number1”:'+$(“#text1”).val()+,“number2”:'+$(“#text2”).val()+'

删除
contentType:'application/json;charset=utf-8',
数据类型:'json',
返回的字符串不是json

您的ajax应该如下所示:

$.ajax({
    url: $("form").attr("action"),
    type: 'POST',
    data:  {  number1: $("#text1").val(),  number2: $("#text2").val() },
     success: function(response){
        console.log(response);
    },

});
您的php喜欢:

if($_POST) echo json_encode($_POST); else echo "No Data Posted!";
用这个

var centercode=$('#WONum').val(); var centerName=$('#WONum2').val()


用下面代码中给定的
myData
替换您的
myData

<script>
var myData = JSON.stringify({ "number1":  $("#text1").val() , "number2": $("#text2").val() });

$.ajax({
    url: $("form").attr("action"),
    type: 'POST',
    success: function(response){
        var p = $("p");
        p.append(response);
        //console.log(response);
    },
    error: function(request,error,msg){
        $("p").html(error+": "+msg);
    },
    data:{myData},
    dataType: 'json'
});
</script>

var myData=JSON.stringify({“number1”:$(“#text1”).val(),“number2”:$(“#text2”).val());
$.ajax({
url:$(“表单”).attr(“操作”),
键入:“POST”,
成功:功能(响应){
var p=$(“p”);
p、 追加(应答);
//控制台日志(响应);
},
错误:函数(请求、错误、消息){
$(“p”).html(错误+”:“+msg);
},
数据:{myData},
数据类型:“json”
});
并使用
echo-json\u-encode($\u-POST)在目标文件中

var myData = { number1: $("#text1").val(), number2: $("#text2").val() };

$.ajax({
    url: $("form").attr("action"),
    method: 'POST',        
    data: JSON.stringify(myData),
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    success: function(response){
        var p = $("p");
        p.append(response);
        //console.log(response);
        },
    error: function(request,error,msg){
    $("p").html(error+": "+msg);
    },
});
使用此代码。
如果添加
console.log(myData),请注意变量myData和JSON.stringify

,您发送的JSON的格式到底是什么?还请注意,如果您直接向
$.ajax
@RoryMcCrossan
console.log(myData)的
数据
参数提供一个对象,这将是更好的做法(并可能解决您的问题)
{“number1”:4534534,“number2”:3543534}
打印到控制台。如果($\u POST)打印($\u POST),则
响应的值是多少?@rorymcrossan I have
;否则回显“无数据发布!”目标php文件中的此代码。获取响应<代码>未发布任何数据console.log(myData)
将`Object{number1=“4534534”,number2=“3543534”}`打印到控制台并得到相同的响应:
parserror:SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符
@A.B.是否删除了数据类型?尝试删除
contentType:'application/JSON;charset=utf-8',
使用
代替
=
我已经删除了
应用程序/json;字符集=utf-8和数据类型。正在获取
未发布数据但这次没有错误。@A.B.试试这个代码,这对我有用。。。删除
contentType:'application/json;charset=utf-8',
这一行并替换
myData
这将数据作为对象而不是JSON发布。我希望数据以JSON的形式发布。获取空白数组作为响应<代码>echo json_编码($_POST)
是我的目标代码。PHP方法参数是什么?
<script>
var myData = JSON.stringify({ "number1":  $("#text1").val() , "number2": $("#text2").val() });

$.ajax({
    url: $("form").attr("action"),
    type: 'POST',
    success: function(response){
        var p = $("p");
        p.append(response);
        //console.log(response);
    },
    error: function(request,error,msg){
        $("p").html(error+": "+msg);
    },
    data:{myData},
    dataType: 'json'
});
</script>
var myData = { number1: $("#text1").val(), number2: $("#text2").val() };

$.ajax({
    url: $("form").attr("action"),
    method: 'POST',        
    data: JSON.stringify(myData),
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    success: function(response){
        var p = $("p");
        p.append(response);
        //console.log(response);
        },
    error: function(request,error,msg){
    $("p").html(error+": "+msg);
    },
});