How to pass data in $ .post ajax with php - php

Good morning people. I'm new to PHP and need help with an API implementation.
I need to pass the the client data, email, and password. When I step directly in PHP it works perfectly. But when I send via data, it returns 500 from the server.
An example:
public function createUser(){
$this->post("api/license-manager/users", [
"email" => "",
"name" => "xxxx 232r4 xx"
return $this->result();
I wish it would work this way:
var data = {
'email': "",
'name': "Xxxxxx 001323"
type: "POST",
url: '/midleware/createuser/',
data: JSON.stringify(data),
success: function(data){
Can someone help me?

Sorry I can't comment, however you need 50 REP. So here is a shot at an answer although the question seems too ambiguous, so I'm making assumptions here. Feel free to add more data so I can give you a hand.
When trying to look at whats happening I'm assuming that createUser() I can't figure out what you are trying to accomplish, it seems like you are mixing JS and php. When you are interacting with the database in laravel you will be inside of an action that started out as some sort of ajax request, and is sent from something in your javascript front end. I'm assuming that this is set up properly as follows from your example.
var data = {
'email': "",
'name': "Xxxxxx 001323"
type: "POST",
url: '/midleware/createuser/',
data: JSON.stringify(data),
success: function(data){
now, you may need to do something with your headers remember you'll need to send a CSRF token somehow if your /midleware/createuser/ route is on the routes/web.php file, or you'll need to send some sort of token if your route is in the routes/api.php section.
Once you hit your route properly and defer it to a controller method for example you can handle it in the controller or defer it to a FormRequest object that has some advantages over a general request.
You might make your controller resolve your request out of the IOC container by having this function in your "UserLicenceController" You can make a request with an artisan command at the terminal
php artisan make:request CreateLicenseRequestForm
Then you can call it in your create function, I use this convention
public function create(CreateLicenseRequestForm $form)
return $form->persist()
Then in your form, set your "$rules" array to validate whatever you need. Like the user only exists once, and has paid or whatever. Your authorize function to make sure you have authorization to actually make the request for example maybe someone is playing around with routes and is trying to grant themselves access on another account.
Then in your persist method once everything is validated and happy, return your response. If it's to a JS request you probably want JSON
something like
public function persist()
return response($message, $statusCode);
This may have been way more than you need or way less depending on your understanding of the steps. Feel free to follow up with more info.
Good luck.


Wordpress REST API custom endpoint for method POST

I am currently working on a project which requires a WordPress website and a simple REST api. I discovered that WordPress has its own REST api and decided to extend its functionality to meet my needs. All I need to do is to have endpoints for GET and POST requests that retrieve/insert data from/to a table which is not directly related to WordPress (but in the same database). I successfully implemented all GET requests, however, I am struggling to get the POST ones working.
I have this route register defined:
register_rest_route('api/v1', 'create-player/', array(
'methods' => 'POST',
'callback' => 'create_player',
The client sends a request through ajax call which is expected to hit the endpoint from the route above. This is the ajax:
method: "POST",
url: '/wp-json/api/v1/create-player/',
data : JSON.stringify(data),
contentType: 'applcation/json',
beforeSend: function (xhr){
xhr.setRequestHeader("X-WP-None", locData.nonce);
success: function(response){
console.log("success" + response);
fail: function (response){
console.log("fail" + response);
I am not sure how to build the POST route register from the REST api, the other GET requests have an attribute args that map directly to the parameters passed in the endpoint. Do I need something like that to handle the request data when using POST? How do I get the data type passed from the ajax and then use it in my function create_player(); The WP REST API documentation seems to be incomplete and all of the information I found uses endpoints for built-in WordPress features such as posts/authors/blogs etc. but I don't need that, I just want to use the provided functionality and create my own interface. Thank you.
in your callback function you can use something like this:
$param = $request->get_param( 'some_param' );
// You can get the combined, merged set of parameters:
$parameters = $request->get_params();
Finally found it! In order to access the body of the POST request use $request->get_body(); in your register_rest_route callback method.

AngularJS with PHP (forms and ajax)

I just started learning AngularJS and will be using PHP for the backend.
I got the basics of Angular working, but now I'm at the point of form submission. Now I'm thinking that I could just do an $.ajax({...}); call to a PHP file and include that function at the bottom of my view.
However, I see there are other ways using controllers, which I'm researching; I just want to know if that would be considered bad practice or if there is a simple way to transition from one method to the other.
Here's what I did now and it's working great in my opinion. Doesn't hurt the integrity of Angular, the form submits, and all is good. Is there a downside to this:
function submitNewPatient(){
url: 'ajax/patients/new_patient.php',
type: 'post',
data: $("#new_patient_form").serialize(),
success: function()
}, error: function()
alert('something went wrong');
You'll want to look into the $http method.
Here's an example:
$'api/user', {params: {id: '5'}
}).success(function(data, status, headers, config) {
// Do something successful.
}).error(function(data, status, headers, config) {
// Handle the error
Along with .get the $http service has .head, .post, .delete, .put, .jsonp. In your case $, the params and URL in the example above evaluate to api/user?id=5
Also, forms in Angular come with an ng-submit directive which is very useful for calling a function on submission. The ng-submit prevents the browser from its default POST action. This way you can use it to call a function on the controller attached to the view.
<form ng-submit="sendForm()" ng-controller="FormSubmission">
<button>send form!</button>

Sencha Touch: How to build a restful proxy url syntax?

Defined as a model and its associations, I wish the http calls to follow best practices of restful. For example, if I make the call
I wish to run a call http defined as
If a call is then put on post with id 32 then the url of reference must be
So I want to avoid using the filter property as is the default for a get
This is because the api rest are already defined and I can not change them.
I would like to build a minimally invasive solution and reading on various forums the best way is to do an ovveride the method buildURL the proxy rest but was not able to retrieve all the data needed to build the final URL. Can anyone give me an example?
Try the following:
window.serverUrl = "192.168.1.XX"
var service = "login.php"
var dataTosend: {
password: "yy"
var methode:"POST" / "GET"
onSucessFunction: function(res) {
onFailureFunction: function(res) {
service: function(svc, callingMethod, data, successFunc, failureFunc) {
scope: this,
useDefaultXhrHeader: false,
method: callingMethod,
url: window.serverUrl + svc,
params: data,
reader: {
type: 'json'
failure: failureFunc,
success: successFunc
I hope this will solve your problem...

How Can I Use a Proxy With Ember Data's RESTAdapter?

Right now, the Ember Data RESTAdapter is making this call:
GET /workshops
I want to make this call:
POST /scripts/server/api_call.php
"http_verb": "GET",
"endpoint": "special_namespace/workshops",
"data": {}
I'm doing stuff like session management, authorization, and OAuth signing in the api_call.php script, which makes the actual RESTful request and returns the result.
What are the steps to extend the RESTAdapter to do this?
I think you'd have to override the serialize and buildURL methods. You could also override the find call directly and bypass all of the unnecessary Ember-Data calls.
But in my opinion, the RESTAdapter is incredibly complicated nowadays. Given that your API is so different, I think you would be better off writing your own adapter from scratch. My custom REST adapter is about 100 lines long, and 20 of those are just $.ajax options. For instance, your find call could be as easy as:
find: function(store, type, id) {
return new Ember.RSVP.Promise(function(resolve) {
var data = {
http_verb: 'GET',
endpoint: 'special_namespace/' + type.typeKey.pluralize(),
data: {}
$.post('/scripts/server/api_call.php', data, function(response) {

Media Types in all backbone.js REST requests

I've got an API setup which uses Media Types in the Accept header for all requests. Getting started I just simply wrapped the fetch and save functions of models, but I was curious if there was a better way rather than having to do that in every model and collection?
var accountsCollection = Backbone.Collection.extend({
model: accountModel,
url: '/api/accounts',
vnd: 'application/',
return resp.Items;
search: function (q) {
headers: { Accept: this.vnd },
data: {
q: q
success: function (data) {
As in the above example, I'm having to wrap the fetch because of the need to have a custom Accept header. In my opinion a perfect solution would be to be able to extend fetch & save to automatically pull the vnd value of the Collection or Model. I hope this better clarifies what I was asking.
Backbone uses the notion of a sync adapter in order to customize your CRUD operation.
Each collection/model can has its own sync method which could apply your custom http adapter.
Your code could look like:
var MyModel = Backbone.Model.extend({
sync: function(method, model, options) {
var options = options || {};
options.Accept = model.vnd;
return Backbone.sync(method, model, options);
Riffing off of Uzi's answer, you might also consider providing your own Backbone.sync function. Model.fetch() ends up delegated to the ├╝ber-sync function, meaning that a patch there to include the Accept header will ensure the header gets passed along with every request your application makes.
A good example of modifying Backbone.sync can be found in the annotated source of the Backbone/localStorage demo.