dongping1689
2016-05-03 08:25
浏览 48

使用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',
    
    点赞 评论

相关推荐 更多相似问题