Call php function in javascript without waiting for response

心不动则不痛 提交于 2020-01-22 03:38:24

问题


I know how to use $.ajax. I have a Codeigniter project so I just call:

url:'<?php echo base_url()."somecontroller/somefunction"?>', 

This is all ok but ajax waits for the response. I just want to the call the url as you would by typing it in your browser. I don't want to wait for the response because the controller does a redirect and then loads a view. Also i need to be able to send some data via POST.

How can I do this?


回答1:


You can use the following for sending asynchronous request with additional parameters.

$.ajax({
    type: "POST",
    url: url,
    data: data, // additional parameters 
    async:true,
    success: function(response){    
    }
});



回答2:


If you want the request to be synchronous, set async:false. For post, type:POST

Refer http://api.jquery.com/jQuery.ajax/




回答3:


Do you can try this ? Change the (alert) function with your function

  $.ajax({
            url: 'test.html',
            async: false,
            success: function () { alert('hello people from the world'); }
        });

Never use async: false. Since Javascript runs on the UI thread, an async: false request will freeze the browser until the server replies



来源:https://stackoverflow.com/questions/15243535/call-php-function-in-javascript-without-waiting-for-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!