weixin_33690963 2016-01-19 16:31 采纳率: 0%
浏览 37

如何在ajax中发送POST数据?

I need to write a script that receives and parses a JSON array within the POST array. In order to do this, I'm first trying to just send any old JSON data to my script so I have something to work with.

My receiving script is in PHP (but alternately could be done in Javascript.) Right now, I'm just serializing the POST array and writing it to a text file to make sure something is coming in. What it's writing is an empty array, though.

I'm trying to send the data using an ajax request. This is what I have at the moment:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script>
        $(document).ready(function() {  
        var jsondata = JSON.stringify({
            val1:"this",
            val2:"that"
        });  

        $.ajax({
            url: "http://api.mydomain.com/test/index.php",
            method: "POST",        
            data: {json: jsondata},
            contentType: "application/json",
            success: function(data){alert(JSON.stringify(data));},
            error: function(errMsg) {
                alert(JSON.stringify(errMsg));
            }
        });
    });
    </script>
</head>
<body>  
</body>
</html>

I've tried lots of variations on this, too, including

  • not stringifying jsondata
  • changing data to this: data: jsondata
  • using type: instead of method:in the request
  • including datatype: "json" in the request

and some other variations I can't even remember at this point. Am I missing something simple? Or is there an easier way to accomplish this?

EDIT: adding my index.php file

if (isset($_POST)){
    // This line is commented out because it breaks it.
    //$jspost = json_decode($_POST['json']);
    $jsser = serialize($_POST);
    echo "I'm here.";
    // Write to text file
    $myfile = "response.txt";
    $fh = fopen($myfile, 'w') or die("can't open file");
    $now = date("n/j/y g:i:s a");
    fwrite($fh, $now."
");
    fwrite($fh, "I received a POST.
");
    fwrite($fh, $jsser);
    fwrite($fh, "

");
    fclose($fh);
}
  • 写回答

2条回答 默认 最新

  • ??yy 2016-01-19 16:46
    关注

    try this

    var jsondata = {
            "val1":"this",
            "val2":"that"
        };  
    
        $.ajax({
            url: "http://api.mydomain.com/test/index.php",
            method: "POST",        
            data: jsondata,
            contentType: "json",
            success: function(data){
               //your success function
            },
            error: function(errMsg) {
                //your error function
            }
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥25 matlab数值溢出该怎么解决?
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据
  • ¥15 multisim中关于74ls192n和DSWPK开关的问题(相关搜索:计数器)
  • ¥15 在误装Windows server2019 后如何利用Windows.old恢复?
  • ¥20 代码实现状态连接包过滤防火墙的设计与实现
  • ¥15 vscode的红色箭头爆红和has no default export报错