简体   繁体   English

如何在jQuery中使用文档准备功能重定向

[英]How to redirect with document ready function in jquery

I want to redirect after call the DOM. 我想在调用DOM后进行重定向。 This is my script. 这是我的剧本。

    <script>
    $(document).ready(function(){
        $.get("<?php echo $url; ?>", function(data, status){
            alert(data);
        });
    });
    </script>

But I want to do like this.It's not working can anyone help me. 但是我想这样做,任何人都不能帮助我。 Iupdated my coding.If i use window.location.href='trip_details.php' and alert box its not working. 我更新了编码。如果我使用window.location.href ='trip_details.php'并且警告框不起作用。

    $username="cde-stng";
    $password="12345666";
    $msg="Welcome To Safetrip Nigeria";

    $url="http://121.241.242.114:8080/bulksms/bulksms?username=".$username."&password=".$password."&type=0&dlr=1&destination=".$mobile."&source=".$mobile."&message=".$msg;



    <script>
    $(document).ready(function(){
        $.get("<?php echo $url; ?>", function(data, status){
            //alert(data);
            window.alert('Trip Closed');
            window.location.href='trip_details.php';
        });
    });
    </script>

You are adding alert() between the function. 您正在函数之间添加alert()

Remove this line window.alert('Trip Closed') window.location.href='trip_details.php'; 删除此行window.alert('Trip Closed') window.location.href='trip_details.php';

and then add this 然后添加

window.location.href='trip_details.php';

What's the use of this $url and $.get here? $ url和$ .get在这里有什么用? Please let me know what exactly you're trying to achieve here. 请让我知道您要在这里实现的目标。

From what I understand, when you load the DOM, you want to show the alert message "Trip Closed", and then redirect to the trip_details.php. 据我了解,加载DOM时,您想显示警报消息“ Trip Closed”,然后重定向到trip_details.php。 If so, there's no need ot this $.get. 如果是这样,则不需要$ .get。

You could try this: 您可以尝试以下方法:

$(document).ready(function(){
  alert("Trip Closed");
  window.location.href='trip_details.php';
});

Hope this helps. 希望这可以帮助。

Peace! 和平! xD xD

I don't see any problem with your code. 我看不到您的代码有任何问题。 You should look into the AJAX request. 您应该调查AJAX请求。 You can try PostMan to perform a GET request and check what gets returned. 您可以尝试PostMan执行GET请求并检查返回的内容。

If there is error related to Access Control you can use dataType: 'jsonp' to your request. 如果存在与访问控制有关的错误,则可以对请求使用dataType: 'jsonp' This solved my problem when I was working on Google API. 当我使用Google API时,这解决了我的问题。

Try this to log the data returned from the server in console: 尝试使用此命令在控制台中记录从服务器返回的数据:

$.ajax({
  method: "GET",
  url: "<?php echo $url; ?>",
  dataType: 'jsonp'
}).done(function(data){
  console.log(data);
});

If there are no errors, you can try this: 如果没有错误,您可以尝试以下操作:

$.ajax({
  method: "GET",
  url: "<?php echo $url; ?>",
  dataType: 'jsonp'
}).done(function(data){
  alert( "Trip Closed" );
  window.location.href='trip_details.php';
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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