AJAX没有发布JSON数据

时间:2016-04-25 10:24:57

标签: php jquery json ajax

我正在尝试使用AJAXJSON将表单数据发布到其他页面。这是我的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',
});

我正在目标页面上使用$_POST函数打印print_r数组,以查看是否已收到任何参数。但我发现它是空的。获得parsererror: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data回复。

4 个答案:

答案 0 :(得分:0)

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

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

删除contentType: 'application/json; charset=utf-8',dataType: '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!";

答案 1 :(得分:0)

使用此

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

                   //Now you have the value of the textbox, you can do something with it, maybe an AJAX call to your server!
                  var centerObj = {};
                  centerObj["centercode"]= $('#WONum').val();
                  centerObj["centerName"]= $('#WONum2').val();

                             var jsonData = ko.toJSON(centerObj);
                             console.log(jsonData); 


                                $.ajax({
                                    url: web_service_url + "centermaster/createCenterMaster", ///"+branchcode+"/"+branchname,
                                    data: jsonData,
                                    type: 'POST',
                                    contentType: 'application/json; charset=utf-8',
                                    dataType: "jsonp",

答案 2 :(得分:0)

使用以下代码中的给定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>

在目标文件中使用echo json_encode($_POST); ...

答案 3 :(得分:0)

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);
    },
});

使用此代码。 注意变量myData和JSON.stringify