简体   繁体   English

在AJAX调用中转移到另一页

[英]Transfer to another page in AJAX call

Hi there is it possible to redirect to another page using ajax? 嗨,有可能使用ajax重定向到另一个页面吗? I have this piece of code that I have been working on to try this. 我有一段我一直在尝试的代码。

<script type="text/javascript">
$(document.body).on('click', '#btnPrintPrev', function() {
    $.ajax({
        url: '/pdfdatacal',
        data: {
          dummydata: "This is a dummy data"
        },
       });
});
  </script>

Now it should be able to carry data to another page and redirect there. 现在它应该能够将数据传送到另一个页面并重定向到另一个页面。 Problem is it doesn't. 问题是没有。

This is what I am using in my route 这就是我在路线上使用的

Route::get('/pdfdatacal', 'GenerateReportController@pdfdatacal');

Then in the controller 然后在控制器中

 public function pdfdatacal(Request $request) {
        return $request->data['dummydata'];

    }

My expected result should be a blank page containing the value of dummydata but it doesn't do that in my code. 我的预期结果应该是一个空白页面,其中包含dummydata的值,但是在我的代码中没有这样做。 How do I accomplish this? 我该如何完成?

Use 采用

window.location.href = "http://yourwebsite.com/pdfdatacal";

In your success call 在您成功的电话中

The idea is that you send data to your controller, it sends back a response, then you redirect from javascript to where you want. 这个想法是,您将数据发送到控制器,它发送回响应,然后从javascript重定向到所需的位置。

$.ajax({
    url: '/pdfdatacal',
    type : 'GET',
    data : {
        dummydata: "This is a dummy data"
    },
    success : function(data) {              
        window.location.href = "http://yourwebsite.com/pdfdatacal";
    }
});

But if your controller does nothing with the data you send, then you don't need to use ajax at all, simple redirect using javascript. 但是,如果您的控制器对发送的数据不执行任何操作,那么您根本不需要使用Ajax,只需使用javascript进行简单的重定向即可。

you could use window.location.assign('your URL here!'); 您可以使用window.location.assign('your URL here!'); in the success. 在成功。

success : function(data) {              
        window.location.assign('your URL here!');
    }

first your ajax must be something like 首先你的ajax一定是这样的

$.ajax({
    url: '/pdfdatacal',
    method: 'post',
    data: { dummydata: "This is a dummy data" },
    dataType: "JSON",
    success: function(response){
        console.log(response); // just to check if the data is being passed
        // do something you want if ever . 
    }
});

then in your routes 然后在你的路线上

Route::post('/pdfdatacal', 'GenerateReportController@pdfdatacal');

in your controller 在您的控制器中

public function pdfdatacal(Request $request) {
    return response()->json($request->dummydata);
}

hope it helps .. 希望能帮助到你 ..

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

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