Make jQuery AJAX have a non-javascript (non-ajax) fallback?

元气小坏坏 提交于 2019-12-03 08:32:59

For a form, you need to supply method and action attributes to the form tag that contains your inputs. Then actually make it submit there via a submit button. Once that works, then override it with your jquery. Et voila.

This is the big thing about progressive web, you can develop pages that work without javascript. One practical way to solve any problem is to figure out what you'd have needed to do if you never used JS in the first place.

Many developers out there don't believe you should even have a fallback though... its seems to be a hot ideological debate.

Of course. Clearly you have something on the back end processing the data sent via AJAX. It shouldn't be a huge hassle to setup your forms such that they send data (generally via a POST) to the same place. Your form would look something like:

<form action="yourscript" method="POST">
    <input ... />
</form>

Then a user without javascript submits the form via a direct HTTP request and a user with javascript has the action intercepted by jQuery.

Write the HTML first (this is your fallback) and make sure that works with form submission, then add the jQuery on top, that way, if your user doesn't have javascript enabled, the form will still work.

HTML

<form action="yourscript" method="POST">
    <input ... />
</form>

JS

$('form').submit(function(e){    
    var form=$(this);
    var form_url=form.attr('action');
    $.ajax({
        url: form_url,
        success: function(data){}
    });
});

If javascript is enabled it'll be submitted through ajax, otherwise will be normally.

One of my favorite plugins is the jQuery Form Plugin. This allows for elegently simple graceful degredation. Just set up your form as if you were writing a non-JavaScript site. (Set up your server to support it as well.) Then, use the form plugin to override the default behavior, without duplicating code or logic to set it up. If JavaScript is enabled, you get the AJAX functionality, otherwise the default functionality remains in place:

<form name="search" action="/search/" method="GET">
    <input name="responseFormat" value="fullHTML" />
    <input name="q" />
    <input type="submit" value="Search" />
</form>
document.search.responseFormat.value = "json";
$(document.search).ajaxForm({
    dataType: "json",
    success: function (data, status, xhr) {
        ...
    },
    error: function (xhr, status, message) {
        ...
    }
});

The url, method, and values to submit are all retrieved by the form plugin from the HTML automatically. The server knows to return just JSON instead of the full HTML page because you modified the responseFormat hidden field value to json with JavaScript.

The easiest way is to have a submit button in a form, then trigger the Javascript when the button is pushed. If Javascript isn't enabled, it'll just submit regularly. If it is, it'll run the Javascript.

Mike

I like to use an iframe, rather than sending non javascript users into the depths of my theme files. That way they can stay on the page they were on too. You just add the "target" attribute to your form. You can also easily distinguish if you are using AJAX by just adding another parameter to the data that you pass in your JS...

var data = form.serialize()+'&ajax=true';
$.ajax({
  type: 'POST',
  url: 'email.php',
  data: data,
  success: function(response){
  alert(response);
  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!