php - AJAX 未发布 JSON 数据

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

我正在目标页面上使用 print_r 函数打印 $_POST 数组,以查看是否收到任何参数。但我发现它是空的。在响应中获取 parsererror: SyntaxError: JSON.parse: JSON 数据第 1 行第 1 列出现意外字符

最佳答案

用这个

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",

关于php - AJAX 未发布 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837892/

相关文章:

javascript - 如何将 data-id 变量从模式传递到 php 页面

java - PHP socket_write 不工作

php - 该路由不支持 GET 方法。 laravel 中支持的方法 : PUT.

jquery - AJAX 网站和动态 HTML Canvas 的 SEO 优化

json - 直接在HTML代码中的manifest.json(没有文件)

php - 警告 : mysql_fetch_array(): supplied argument is not a valid MySQL result - PHP

javascript - 从 URL 中查找子字符串

php - 向另一个网页传递参数

mysql - 如何将 SQL 查询结果转换/复制为 JSON?

python - 迭代列表并按位置从嵌套字典返回值