Matthew
Matthew

Reputation: 59

jQuery Ajax returning 404 when method = post

So I have an ajax script that runs, it looks like this:

            jQuery.ajax({
                url: 'http://localhost/?page_id=104256',
                type: 'POST',
                data: { name : 'name2' },
                success: function (data) {
                    alert(data);
                },
                error: function(xhr, desc, err) {
                    console.log(xhr);
                    console.log("Details0: " + desc + "\nError:" + err);
                },
            }); 

This runs fine but returns a 404 from the page set as the 'url' If I remove 'type: post'

Upvotes: 0

Views: 15667

Answers (3)

Matthew
Matthew

Reputation: 59

It turns out I forgot to add the name="" parameter in my input types. Doh!

Upvotes: 1

steve
steve

Reputation: 51

If type: 'POST' is omitted, jQuery is treating it like a GET request, which it defaults to see the docs, where the resource may not exist therefore resulting in a the 404 you're seeing.

Upvotes: 1

Pratik
Pratik

Reputation: 954

Here your method: 'Post', Type is something what you want to get in return like text

 jQuery.ajax({
                url: 'http://localhost/?page_id=104256',
                method: 'POST',
                data: { name : 'name2' },
                success: function (data) {
                    alert(data);
                },
                error: function(xhr, desc, err) {
                    console.log(xhr);
                    console.log("Details0: " + desc + "\nError:" + err);
                },
            }); 

Upvotes: 3

Related Questions