带有数据的jQuery Ajax Post

编程入门 行业动态 更新时间:2024-10-27 08:28:34
本文介绍了带有数据的jQuery Ajax Post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么不起作用.我试图通过一些参数发生按钮的onclick时调用php文件.它会一直执行到jsfile.js中的alert语句为止.之后,ajax部分将无法执行..帮帮我..预先感谢..

why it isn't working. Am trying to call a php file when onclick of a button occurs with some parameters. It is getting executed till alert statement in jsfile.js. After that the ajax part is not getting executed.. Help me out.. Thanks in advance..

main.html

<!DOCTYPE html> <html> <head> <title></title> <script src="jsfile.js"></script> <script src="ajax.googleapis/ajax/libs/jquery/3.1.1/jquery.min.js"></script> </head> <body> <button onclick="cart(0)"> hi </button> <p id="disp"></p> </body> </html>

jsfile.js

function cart(id1) { var id=id1; alert("enterd "+id); document.getElementById("disp").innerHTML ="hi"; $.ajax({ url:"/add.php ", type:"POST", data:{ item_id: id, }, success:function(response) { //document.getElementById("total_items").value=response; document.getElementById("disp").innerHTML =response; }, error:function(){ alert("error"); } }); }

add.php

<?php if(isset($_POST['item_id']) && !empty($_POST['item_id'])){ //if(count($_POST)>0) echo "success"; exit(); } ?>

推荐答案

在您的jsfile.js文件中,请更正我在以下代码中作为注释提及的以下几点.

In your jsfile.js file, please correct the following points which I have mentioned as comments on the following code.

function cart(id1) { var id=id1; alert("enterd "+id); document.getElementById("disp").innerHTML ="hi"; $.ajax({ url:"/add.php ", method:"POST", //First change type to method here data:{ item_id: "id", // Second add quotes on the value. }, success:function(response) { document.getElementById("disp").innerHTML =response; }, error:function(){ alert("error"); } }); }

更多推荐

带有数据的jQuery Ajax Post

本文发布于:2023-11-24 22:35:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   jQuery   Post   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!