简体   繁体   中英

$.post doesn't work on locally deployed site

I recently "converted" an HTML website to web forms. By convert, I mean I opened the website in Visual Studio 2010, added a web.config file to allow HttpPost protocol, and called it converted. However, my form doesn't want to post to my .aspx page. What am I missing? When I build the app, there is no binary created to deploy to my local IIS (7.5 on Windows 7)

<form name="register2" method="post" action="#" onsubmit="return false;">

Then my $.post is in my $(function() {...

$('form[name="register2"]').submit(function () {
    var $registerForm2 = $('form[name="register2"]');
    if ($registerForm2.valid()) {
        $.post({
            type: 'POST',
            url: 'CreateAccount.aspx',
            data: $(this).serialize()
        });
    } else { //do validation
        $registerForm2.validate();
    }
});

When I submit the form, Chrome tells me the request URL is URL:http://localhost/mysite/[object%20Object] and receives error code 404. The page CreateAccount.aspx does exist.

I see the problem now. the $.post() method doesn't support taking an options object as a parameter.

from the documentation :

$.post('ajax/test.html', function(data) {
  $('.result').html(data);
});

so change your code to be:

 $.post( 'CreateAccount.aspx', $(this).serialize() );

and give that a try.

You will need to change the extension of your pages to .aspx so the ASP.NET runtime will process it. You will also need the <%@ Page %> directive at the top of the page to indicate the page should be compiled.

Also, you will need to add runat="server" to your FORM tag.

Try adding a new WebForm page to the project to see how it gets set up by default, and verify that that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM