Ajax request is not sending data over POST

GrumpyCrouton

I've got the following Javascript function

http_post = function (url, data, success) {
    var json_data = JSON.stringify(data);

    return $.ajax({
        type: "POST",
        url: url,
        data: json_data,
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: success,
        fail: function(sender, message, details) {
            console.log(sender, message, details);
        }
    });
}

Which I am calling like this

$('#teammate_search_input').keyup(delay(function (e) {
    var value = $(this).val();

    http_post("{{ \Path::href('roster', 'search_name') }}", {name: value}, function(data) {
        console.log(data);
    });

}, 500));

Which is hitting this PHP script

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $download = $this->model('download');
    $results = $download->search_teammate($_POST['name']);
    echo json_encode(['term' => $_POST['name'], 'results' => $results]);
}   

Which is supposed to simply pull a list of users with a name that matches the input. This is all working fine (I get the response I am expecting), except the term is always set to null, so it returns a list of all users.

Result:

{term: null, results: Array(5)}

It appears as though the data is not being sent over the request.

Things I've tried:

  • Changed {name: value} to {name: 'test'} (2nd param of function call)
  • Removed the var json_data line and replaced data: json_data, with data: data

Why is my data not being sent over the request?

Rory McCrossan

Is your PHP endpoint configured to accept JSON? I don't believe this is the case by default (assuming you're not using a server-side library or framework).

You can instead send form-urlencoded data. To do that remove contentType: "application/json;charset=utf-8", and use data: data without calling JSON.stringify() on it, like this:

let http_post = function(url, data, success) {
  return $.ajax({
    type: "POST",
    url: url,
    data: data,
    dataType: "json",
    success: success,
    fail: function(sender, message, details) {
      console.log(sender, message, details);
    }
  });
}

Note that this makes your function almost entirely redundant, but I left it in anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sending an image and JSON data to server using Ajax POST request?

Ajax POST not sending data when calling 'request.POST' in Django view

Objective c post request not sending the data

Sending data over HTTP using POST with Arduino

AJAX Post request not sending values to Django View

Sending URI data via AJAX POST

POST request not sending data - FIXED

Angular 7 not sending HTTP Post request data

Jquery Ajax not sending multiple data via post request

Problem sending data through post request in Python

Error while sending POST request with data

sending a post request to an external API and extracting data

Error while sending data with POST request in Android

Sending post request with both data and JSON parameters

Sending image file and data in one post request

JQuery Ajax request not sending through post variables

Chrome Extension post request not sending data

jQuery or AngularJS Ajax "POST" request to server using code below however do not sending param data to server.

QNetworkAccessManager is not sending data part of POST request

AJAX Post Request over VPN stripping request data

JQuery Ajax PUT request not sending data to the server

No data response when sending ajax request to php

Sending ajax post request, but $_POST in PHP is always empty

Sending a POST request with Ajax on button click

Sending form data to a custom post type WP with AJAX request

Javascript POST request not sending data

Sending post request using ajax and load the data into the same php file

Sending data in json object in angular post request

Why is empty data being transmitted when sending an ajax request to jquery post?