2

When I try to access the object property outside the ajax when getting the value of an input it returns the correct value. But when I try to log and append that value inside the success function it returns undefined. How do I address this type of issue? It seems that object is local and cannot accessed by the ajax? Below is my code.

$('#add-order').on('click',function(){

    //create an object for orders
    var order = {   

        name : $('#name').val(),
        drink : $('#drink').val()
    };

    $.ajax({
        url : 'api/orders.json',
        type : 'POST',
        data : order,
        dataType : 'json',
        success : function(newOrder){
            console.log(newOrder.name);
            $('#orders').append('<li>' + newOrder.name + ' : ' + newOrder.drink + '</li>');
        },
        error: function(){
            console.log('error connecting');
        }
    });

});

Here's the index.html

<h4>Add a Coffee Order</h4>
<ul id="orders">

</ul>

<p><input type="text" id="name"></p>

<p><input type="text" id="drink"></p>

<button type="submit" id="add-order">Add</button>

Since I needed a server side language I used PHP. However I cannot write the data coming from the ajax using the JSON.stringify method.

    <?php

   if (isset($_POST['submit'])) {
        $orders = $_POST['data'];
        $orderFile = fopen('api/orders.json', 'w');

       fwrite($orderFile, $orders);
       fclose($orderFile);
   }

I also updated my code on top.When I hard coded any string to fwrite($orderFile, "my orders") it will write on the orders.json however when I used the $orders it's not working. Am i missing something here?

2 Answers 2

3

newOrder is the JSON returned by the AJAX call and it can have differnet keys depending upon what you are doing, if you want to print the order object then do

$('#orders').append('<li>' + order.name + ' : ' + order.drink + '</li>');
Sign up to request clarification or add additional context in comments.

13 Comments

Ah got it. Yeah that's why when I log the newOrder it returns the data in it. Thank you so much.But one last question will the value I inputted in the input will be written to order.json file? If not how will I do it?
@JayGorio: Well, it might be written to the json file. That's what the server needs to do, all you can do clientside is to pass the data to the server through that POST request.
@Bergi i do not think it is being written, because the file name is .json and it can not do any server side processing.
@Beigi yeah its not being written. Ho do I deal with this?I thought it will automatically written when using POST method but it's not.
@void but if I change the extension will it be written?
|
0

But when I try to log and append that value inside the success function it returns undefined.

That is because of your json file orders.json or the logic of serverside code. In the returned json newOrder in the success callback the object will not be having a key name/drink so that causes in undefined.

So, the thing is you have to return it from serverside with the specified keys, which should look like this:

{ "name":"Mango", "drink":"shake" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.