Ajax changing get to post and header data disappears


Joined
Dec 14, 2020
Messages
3
Reaction score
0
I'm having an issue with ajax. I have an api set up that takes json as a post value and returns json as a result. It works fine using the postman app to test it. It also works when I send the data from an ajax call as get. But as soon as I call it as post data, the post data is empty.

Using the console in Chrome I can see the values of the post and get vars.

The ajax code is:

Code:
        $( document ).ready(function() {
            var the_data = {
                request: {.....},
                limits: {.....},
                .....
            };


            $.ajax({
                url: "http://localhost/api/index.php",
                type: "POST",
                datatype: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(the_data),
                success: function(data){
                    console.log(data);
                },
                error: function(jqXHR, textStatus, errorThrown){
                    console.log(errorThrown);
                }
            });
        });

Any ideas why the post var isn't working?
 
Ad

Advertisements

Joined
Nov 13, 2020
Messages
80
Reaction score
14
Writing just so you know someone here has read your question. I really can't answer it because it's about jQuery and I have stopped wasting my time re-reading about jQuery when JS does a better job. BUT one thing that caught my eye was the url
"http://localhost/api/index.php"
The index file is the main file. It is called by the server when you type an address into the browser address bar. I would change that and place your PHP code into that file instead of your main file.
 
Joined
Dec 14, 2020
Messages
3
Reaction score
0
Thanks for the reply. I've set up an index2.php already that just returns the value in the post var and the same thing happens.
 
Joined
Nov 13, 2020
Messages
80
Reaction score
14
God, I hate index2.php also. But guess it works.
Here is simple JS
main html file
Code:
<!DOCTYPE html>
<html>
<body>

<h2>The XMLHttpRequest Object</h2>

<p id="demo">Let AJAX change this text.</p>

<button type="button" onclick="loadDoc()">Change Content</button>

<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "ajax_info.txt", false);
xhttp.send();
document.getElementById("demo").innerHTML = xhttp.responseText;
}
</script>

</body>
</html>

And
ajax_info.txt
Code:
["apple", "apricot", "avocado", "banana", "blackberry", "List2"];
Works with GET as well as POST
 
Joined
Dec 14, 2020
Messages
3
Reaction score
0
Thanks again.

That's fine for html but both my client side and api are PHP so reading from a text file isn't an option.

The jQuery is far more useful as it splits our the response and error functions too.
 
Ad

Advertisements

Joined
Nov 13, 2020
Messages
80
Reaction score
14
Last night as I was drifting off my brain said I should have modified that code to call a php file and not a txt file. Then I turned over and thought "I'm sure Stooshie could do that." But I came here to do that - alas too late.

I was hoping a jQuery user would be here by now and rescue me. My sins are catching up with me I guess. I don't want to start a fight about frameworks. jQuery's error function is written in raw JS. You can write this code up yourself and save it as a snippet to be used with your raw AJAX code. To me not having to download extra code that's over 86K to do a simple function is the way to go.

FYI: I went to run your code. Not enough info to do that. If you post all the necessary files I will take the time to run it and see what I can do in the lack of jQuery users.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top