简体   繁体   English

使用jQuery AJAX请求-POST到php

[英]Using jQuery AJAX requests - POST to php

I am creating a cross-platform application using HTML, CSS, and jQuery/JavaScript. 我正在使用HTML,CSS和jQuery / JavaScript创建跨平台应用程序。 I am using AJAX requests to get data from a database, as well as posting data to a database. 我正在使用AJAX请求从数据库获取数据,以及将数据发布到数据库。 I have gotten the AJAX 'GET' request method to work. 我已经使用了AJAX“ GET”请求方法。 This is my 'GET' method (this is in a script I have on one of my HTML pages). 这是我的“ GET”方法(在我的HTML页面之一中的脚本中)。

GET method GET方法

var outputHeader = $('#outputHeader');
var outputDes = $('#outputDes');
$.ajax({
    url: '/currentEvent.php',
    type: 'GET',
    //data: 'json',
    datatype: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 5000,
    success: function (data) {
        outputHeader.html("");
        outputDes.html("");
        for (var i in data) {
            outputHeader.append("<h2>" + data[i].Event_Name + "</h2>" + "<p>" + data[i].Event_Date + "</p>" + "<p>" + data[i].Event_Time + "</p>" + "<p>" + data[i].Event_Venue + "</p>" + "<p>" + "Organised by &nbsp" + data[i].Event_Organiser + "</p>");
            outputDes.append("<center>" + "<img src='" + data[i].Event_Image + "'/>" + "</center>" + "<p>" + data[i].Event_Description + "</p>")
        }
    },
    error: function () {
        outputHeader.html('<p>There was an error loading the data.</p>');
        outputDes.html('<p>There was an error loading the data.</p>');
    },
});

It is the 'POST' method that is troubling me. 困扰我的是“ POST”方法。 This is the code I have for the 'POST' method (this is in a script I have in one of my HTML pages). 这是我为“ POST”方法编写的代码(这是在我的HTML页面之一中的脚本中)。

POST method POST方法

var urlID = 1;
$.ajax({
    url: '/currentEvent.php',
    type: 'POST',
    data: 'urlID=' + urlID,
    datatype: 'text',
    success: function (data) {
        console.log(data);
    },
});

The AJAX request is posting to this .php page. AJAX请求正在发布到此.php页面。

currentEvent.php page currentEvent.php页面

        $eid = $_POST['urlID']];
        $query = "SELECT * FROM Event WHERE Event_ID = {$eid}";
        $getData = sqlsrv_query( $conn, $query );
        if ($getData === false) {
            die(print_r(sqlsrv_errors(), true));
        }           
        $records = array();

            while($row = sqlsrv_fetch_array($getData, SQLSRV_FETCH_ASSOC)) 
            {
                $records[] = $row;
            }
        //echo json_encode($getData);
        echo json_encode($records);

When I run the HTML page (I am using a server), it doesn't set "$eid" as 1. I have the 'POST' method before the 'GET', because I have to post the id to the php page so that it can run the query and return the data back to the application. 当我运行HTML页面(我正在使用服务器)时,它没有将“ $ eid”设置为1。我在“ GET”之前有“ POST”方法,因为我必须将ID发布到php页面以便它可以运行查询并将数据返回给应用程序。 The result is an array of variables in the 'GET' method being undefined. 结果是未定义'GET'方法中的变量数组。 I would post a picture, but I cannot because I am new. 我会发布图片,但是我不能因为新手而发布。

I have tried the solutions that were suggested by other users, but it didn't work. 我尝试了其他用户建议的解决方案,但没有用。 I am a bit of a newbie when it comes to developing in jQuery and JavaScript, so it is taking me a while to understand it. 在使用jQuery和JavaScript进行开发时,我还是一个新手,所以花了一些时间来理解它。 Is there anything I am doing wrong? 我做错了什么吗?

From what I know, in post you need to set the request header of "content-type" to "multipart/form-data". 据我所知,在发布后,您需要将“内容类型”的请求标头设置为“ multipart / form-data”。 You would do this like so: 您将这样做:

$.ajax({
         url: "/currentEvent.php",
         type: "POST",
         data: 'urlID=' + urlID,
         datatype: 'text',
         beforeSend: function(xhr) {
             xhr.setRequestHeader('content-type', 'multipart/form-data');
         },
         success: function (data) {
             console.log(data);
         },
      });

Hope this helps, 希望这可以帮助,

Awesomeness01 太棒了01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM