weixin_33712987 2017-11-22 15:23 采纳率: 0%
浏览 4

jQuery-ajax不成功

I am quite new to to ajax, just learning it, and made a simple page on localhost to test gets and posts from/to json file in the same folder.

While GET is working smoothly, I cannot figure out, why post doesn't happen if I click the button I assigned this function to. Pls take a look into my code and help.

element = $("#mylist");
var item2 = $("#mytable");

$.ajax({
  type: "GET",
  url: "data.json",
  success: function(response) {
    $.each(response, function(i, item) {
      element.append("<li>" + item.fname + " " + item.lname + "</li>");
      item2.append("<tr><td>" + item.lname + "</td>" + "<td>" + item.fname + "</td></tr>");
    });
  },
  error: function() {
    alert("error");
  }
});

$("#additem").on('click', function() {
  var $fname = $("#fname");
  var $lname = $("#lname");
  var $city = $("#city");
  var order = {
    fname: $fname.val(),
    lname: $lname.val(),
    city: $city.val()
  };
  console.log(order);

  $.ajax({
    type: "POST",
    url: "data.json",
    data: order,
    succes: function() {
      console.log("succes");
    },
    error: function() {
      console.log("no success");
    }
  });
});

JSFiddle

  • 写回答

1条回答 默认 最新

  • weixin_33738578 2017-11-22 15:34
    关注

    The problem is you are trying to post to a .json file, like Patrick Evans says in the comments. You need to do the post to a script, in PHP you could do something like this:

    $order = $_POST['order'];
    // Do something with order...
    echo $order; // or echo success message
    

    Of course for this to work you will need PHP to be running on your server (localhost).

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作