简体   繁体   English

Javascript重定向成功实现Ajax

[英]Javascript redirect on Ajax success

I have a quiz type application. 我有一个测验类型的应用程序。 Each question has two answers with a value of 1 or 2. When the quiz is complete, if they have a score lower than 10, they get redirected to a page. 每个问题都有两个值为1或2的答案。测验完成后,如果分数低于10,则会将他们重定向到页面。

This is my code for this part. 这是我这部分的代码。

while (n < numResults) {
    increment = minScore + (interval * n);
    if (totalScore <= increment) {
        if(totalScore <= 10) {
            $.ajax({
                method: "POST",
                url: "handleData.php",
                dataType: "json",
                data: { answers: ansArray, page: window.location.href }
            })
            .done(function( msg ) {
                window.location.href("www.page2.html");
            });
        } 
        return;
    } else {
        n++;
    }
}

I have a few things I am trying to solve. 我有几件事想解决。 Firstly, before the redirect, some data (answers and url) is posted to PHP so I can process it. 首先,在重定向之前,一些数据(答案和URL)已发布到PHP,因此我可以对其进行处理。 One thing I pass is the current window url. 我通过的一件事是当前的窗口URL。 The reason I do this is because the url has a format like 我这样做的原因是因为url的格式如下

www.page1.com?a=1&b=2&c=3 www.page1.com?a=1&b=2&c=3

In PHP, I parse this url and grab the values. 在PHP中,我解析此URL并获取值。

My first problem is that although the data is successfuly sent to PHP and handled, and returns a response of Success, the done function never seems to fire, therefore no redirect occurs (I put an alert in this function to ensure it is not firing). 我的第一个问题是,尽管数据已成功发送到PHP并进行了处理,并返回了成功的响应,但完成的函数似乎从未触发,因此不会发生重定向(我在此函数中放置了警报以确保未触发) 。 In PHP, after I process the data, I do this 在PHP中,处理完数据后,我执行了此操作

var_dump($response); //Outputs Success
return json_encode($response);

The second thing I am trying to work out is the redirect url (page2.html). 我要解决的第二件事是重定向URL(page2.html)。 Within this page, I have a button. 在此页面中,我有一个按钮。 This button has a standard link, and then I need to give it some params from the initial url. 该按钮具有标准链接,然后我需要从初始URL中为其提供一些参数。 So this pages button might be something like 因此,此页面按钮可能类似于

www.externallink.com?a=1&c=2 www.externallink.com?a=1&c=2

How can I get the original URLs params into a button on the redirected url? 如何将原始URL参数放入重定向的URL上的按钮中?

Thanks 谢谢

USE below function insted of done: 使用下面的功能来完成:

 $.ajax({
        method: "POST",
        url: "handleData.php",
        dataType: "json",
        data: { answers: ansArray, page: window.location.href }
        success:function(data){
            window.location.href("www.page2.html");
        });
    })

For your 1st part: 对于您的第一部分:

Try putting the error function of jQuery ajax call. 尝试放置jQuery ajax调用的错误功能。 Sometimes when the return type of result does not match with the expected datatype of ajax call, then result comes in the response of error. 有时,当结果的返回类型与ajax调用的预期数据类型不匹配时,结果将返回错误响应。

error: function (data, status, e) {

        }

For your 2nd part: 对于第二部分:

Attach click event for the button in the handler and read the current URL query string using window.location.search and then redirect using 在处理程序中为按钮附加click事件,并使用window.location.search读取当前的URL查询字符串,然后使用进行重定向

window.location = newURL + "extra query params";
// Assign handlers immediately after making the request,
// and remember the jqXHR object for this request
var jqxhr = $.ajax( "example.php" )
  .done(function(data, textStatus, jqXHR) {
    alert( "success" );
  })
  .fail(function(jqXHR, textStatus, errorThrown) {
    alert( "error" );
  })
  .always(function(data|jqXHR, textStatus, jqXHR|errorThrown) {
    alert( "complete" );
  });

If you .done() callback never invoked, try to set debuggers or alerts inside .fail() or .complete() callback functions. 如果您从未调用过.done()回调,请尝试在.fail().complete()回调函数中设置调试器或警报。 Check if you have an error during ajax call and at all if the call has complete statement. 检查ajax调用过程中是否有错误,以及该调用是否具有完整的语句。 Here more information: http://api.jquery.com/jquery.ajax/ 这里的更多信息: http : //api.jquery.com/jquery.ajax/

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

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