dongping1689 2016-05-03 08:25
浏览 50
已采纳

使用JQUERY的AJAX POST请求

So I'm trying to implement a calendar - fullCalendar to be precise - with AJAX so that Ill be able to store events in database. I followed this http://developer-paradize.blogspot.si/2013/06/jquery-fullcalendar-integration-with.html , but it didn't work with adding events. I modified my code a bit and PHP script that stores data gathered through POST request in database, seems to work fine when I send request with Postman, but when I try to post with AJAX nothing happens. PHP script for adding event data in database:

<?php
$title = $_POST['title'];
$start = $_POST['start'];   
$end = $_POST['end'];   
$url = $_POST['url'];

try {
    $bdd = new PDO('mysql:host=localhost;dbname=tracksto', 'admingZZX92g', 'LwQ1Wwi6N2I3');
} catch(Exception $e) {
    exit('Unable to connect to db.');
}

$sql = "INSERT INTO evenement (title, start, end, url) VALUES (:title, :start, :end, :url)";
$q = $bdd->prepare($sql);
$q->execute(array(':title'=>$title, ':start'=>$start, ':end'=>$end, ':url'=>$url));

My AJAX call:

var data = { "title":"Gregor", "start":"2016-05-10 00:00:00", "end":"2016-05-10 00:00:00", "url":"www.renting.si" };
       $.ajax({
           url: '"http://localhost/gui/calendar/add_events.php',
           contentType: "application/x-www-form-urlencoded; charset=utf-8",
           data: data,
           type: "POST",
           success: function(json) {
                alert('Added Successfully');
           }

      });

If I have variables title,start,end,url defined and set data to:

data: 'title='+title+'&start='+start+'&end='+end+'&url='+url,

same thing, nothing happens. In POSTMAN the request looks like this: POSTMAN POST request

The script normally processes this request and stores data in database. Is my AJAX call wrong or is there something else?

  • 写回答

1条回答 默认 最新

  • dscjp19831212 2016-05-03 08:38
    关注

    You can try that:

    var data = {
        "title":"Gregor",
        "start":"2016-05-10 00:00:00",
        "end":"2016-05-10 00:00:00",
        "url":"www.renting.si"
    };
    var url = "http://localhost/gui/calendar/add_events.php";
    
    $.post(url, data, function (json) {
        alert("Success here");
    });
    

    Care, in you code you have a double quotation mark in this line before 'http':

     url: '"http://localhost/gui/calendar/add_events.php',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建