如果使用JQuery async为true,则使ajax调用工作

时间:2017-10-17 23:49:50

标签: jquery ajax asynchronous

我已经使用JQuery使Ajax调用工作并使异步错误。但是,我在几个博客上看到我应该使用Javascript的异步性质。我应该怎么做这个Ajax调用才能让它使用异步工作?

<script>
        $(document).ready(function () {


        $('#new-search').click(function(){
            var userInput = $('#address').val();
            if(!userInput){
                userInput = $('#address').attr('placeholder');
            }
            if(Number(userInput)){
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    type: 'POST',
                    url: '/userprofilepage',
                    async: false,
                    data: {
                        val: userInput,
                        field: 'zip_search'
                    },
                    success: function (msg) {
                        console.log("Job Matcher page has been updated");
                        location.reload();
                    },
                    error: function (msg) {
                        console.log("Job Matcher page has not been updated");
                        var message = msg["responseJSON"]["val"].toString();
                        message = message.replace('val', 'zip code');
                        showNotify(message, 'error');
                    },
                });
            } else{
                alert("false")
            }
        });
    });
</script> 

0 个答案:

没有答案