Dominik Makovec
Dominik Makovec

Reputation: 31

Using AJAX to post data into API

I have a script that uses AJAX to comunicate with PHP based API. First part loads trade history:

$(document).ready(function () {
                var orders = $('#History ul');
                var user = "<?php echo $user; ?>";

                $.ajax({
                    type: "GET",
                    url: "api.php",
                    data: {
                        user: user
                    },
                    success: function (response) {
                        console.log(response);
                        var res = JSON.parse(response);
                        $.each(res, function (index, value) {
                            console.log(value);
                            if(value['PL']>=0){
                            orders.append("<li style=\"color:green;\">" + value['User'] + "</li>");
                        }else{orders.append("<li style=\"color:red;\">" + value['User'] + "</li>");}
                        });
                    }
                });

Second part posts a trade to database:

 $("#submit").click(function(){
                        //event.preventDefault();
                        var oPrice = newOrder.elements["oPrice"].value;
                        var cPrice = newOrder.elements["cPrice"].value;
                        var oType = newOrder.elements["oType"].value;;
                        var oSymbol = newOrder.elements["oSymbol"].value;
                        var oAmount = newOrder.elements["oAmount"].value;

                        var json ={
                            'user': user,
                            'oPrice': oPrice,
                            'cPrice': cPrice,
                            'oType': oType,
                            'oSymbol': oSymbol,
                            'oAmount': oAmount};

                        alert(JSON.stringify(json)); //---check zda je naplněný
                        $.ajax({
                            type: "POST",
                            url: "api.php",
                            data: json,
                            success: function (response) {
                                alert(response);
                            }
                        });
                    });

The problem is, that when i press the button and send json, its missing the 'user' data and looks like this:

TraderBook.php?oPrice=1&cPrice=1&oType=LONG&oSymbol=1&oAmount=1

I have no idea why does ajax exclude it. The json variable has it filled out

Upvotes: 3

Views: 129

Answers (2)

Dominik Makovec
Dominik Makovec

Reputation: 31

I was wrongchecking the problem a mistook data from a form for the json. Problem was inside the API --> There was a tabulator in a SQL command..

Thanks to everyone for suggestions.

Upvotes: 0

Machavity
Machavity

Reputation: 31654

I think your problem might be here

$(document).ready(function () {
    var user = "<?php echo $user; ?>";

var is the JS scoping declaration. So you're limiting your user value to just the anonymous function being triggered by the page DOM load completing. What you should do is try scoping it outside the function

var user; //global scope
$(document).ready(function () {
    user = "<?php echo $user; ?>";

This way, when your $("#submit").click(function() fires, there's a value to feed into your script.

Upvotes: 1

Related Questions