doushitang4276 2017-05-07 18:53
浏览 28

jquery ajax帖子不起作用

I read many posts on this issue but I can not solve the problem. It seems that ajax() works fine, but the .php file fails to update the SQL database and returns no results to the ajax() function.

This is my code:

    var datastring=JSON.stringify(utnew)

    $.ajax({    
            type: "POST",
            url: "myulr-php",       
            data: datastring,
            dataType: "json",
            success: function(resto){
                       if (resto=="success"){
                         $("#pop-confermadatiuser").html("OK"); 
                       }else{ 
                         $("#pop-confermadatiuser").html("KO");
                       };
                    }

    });

The php code work, i have try. this is the code:


    header("Access-Control-Allow-Origin: *");
    $con = mysqli_connect("sql","xxx","xxx","xxx") or die ("could not connect database");

     $ck=1 ;
     if(isset($_POST['id']))
     {
        $id=$_POST['id'];
        $nome=$_POST['nome'];
        $cognome=$_POST['cognome'];
        $matricola=$_POST['matricola'];
        $mail=$_POST['mail'];
        $password=$_POST['password'];
        $ck=2
        $qry="UPDATE user SET nome='".$nome."', cognome='".$cognome."', mail='".$mail."', password='".$password."' where id=".$id." and matricola=".$matricola;

     $q=mysqli_query($con,$qry);
     if($q) {
             echo "success";
     } else {
             echo "error";
     };
      mysqli_close($con);
     };

     if(ck==1){
               echo "error2";
     };

  • 写回答

1条回答 默认 最新

  • douju2012 2018-11-19 02:39
    关注

    The jQuery code is sending the POST data as a JSON string, but your PHP code is looking for standard key-value pairs.

    You can change the Ajax call to send in key-value format. I can't see what utnew refers to in your code, usually that would be a Javascript object or the result of serializing an HTML form with $.serialize():

    $.post("myurl.php", utnew, "json")
      .always(function(response) { console.log(response); }
    

    Or you can change the PHP side to expect a JSON-encoded string in the POST payload rather than encoded key-value pairs:

    <?php
        ...
        $data = json_decode(file_get_contents('php://input'));
        if (isset($data['id'])) { ... }
    

    Whichever you choose, the problem you're having is the Ajax call sending a JSON-encoded string as the POST payload, but PHP expecting key-value pairs in $_POST.

    The "json" data type argument to jQuery $.ajax or the shortcut method $.post tells jQuery what format to expect the return value in, so your PHP code should return a JSON-encoded string. Your PHP code is sending back a plain string, so you should either tell jQuery to expect that:

    $.post(url, data, "text")

    or have PHP send back a proper JSON-encode object:

    echo json_encode(["result" => "success"]);

    If you have PHP error logging on (you should) to a file you can debug your API endpoint with error_log, i.e. error_log(print_r($_POST,1)); would have given you some clues.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度