admin管理员组

文章数量:1355093

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'

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'

Share Improve this question edited Apr 8, 2015 at 11:27 CommunityBot 11 silver badge asked Apr 8, 2015 at 9:11 MatthewMatthew 591 gold badge2 silver badges9 bronze badges 3
  • 2 What is your backend ? it may be possible that the server side is not allowing post action ? – Igoris Commented Apr 8, 2015 at 9:20
  • Wordpress backend. But it works fine with my other script which runs almost the exact same code. @IgorisAzanovas – Matthew Commented Apr 8, 2015 at 9:24
  • Try getting post data through general <form> tag with submit button to check if all data that i'm passign is ok and it load without ajax. – Igoris Commented Apr 8, 2015 at 10:04
Add a ment  | 

3 Answers 3

Reset to default 3

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);
                },
            }); 

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.

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

本文标签: javascriptjQuery Ajax returning 404 when methodpostStack Overflow