【问题标题】:Ajax jquery redirecting is not workingAjax jquery 重定向不起作用
【发布时间】:2016-08-11 07:53:16
【问题描述】:

当用户登录时我有一个登录表单,他必须重定向到参与者screen.php

Ajax 脚本

    $.ajax({
      url: "controller.php",method: "POST",
      data: { loginData: $("#loginForm").serialize(),'action': 'login' },
      dataType: "text",
      success: function(response) {
        $("#showMessage").html(response);
        if (response == 'success') {// response coming as success but not redirecting
 // redirecting script
        }
      }
  });

【问题讨论】:

  • 我不是 PHP 程序员,但看起来对 SQL 注入开放!
  • 我是 PHP 开发人员,我确认 ;-)

标签: jquery ajax redirect


【解决方案1】:

当您使用 ajax 时,您不能使用 header() 重定向。相反,当请求成功处理后,您必须在 ajax 的 javascript 部分中重定向。参见例如How to redirect to another webpage in JavaScript/jQuery?

请注意,您必须从您的 php 脚本(例如 json)发回信息,以便您可以在 javascript 中采取适当的操作。

除此之外:

  • 您遇到了 sql 注入问题,您应该使用准备好的语句,而不是直接将变量注入到您的查询中。
  • 您不应使用md5() 进行密码散列。参见例如How can I store my users' passwords safely?

【讨论】:

  • 完整 url 或页面 url 意思是 amerytech.net/learningcurve/participant-screen.php 或参与者-screen.php ....? @jeroen
【解决方案2】:

jquery 不支持header() 你需要用户windows.location.href

window.location.href="your url";

【讨论】:

【解决方案3】:

你可以试试下面的重定向方法

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

【讨论】:

    【解决方案4】:

    我找到了解决办法

    $.ajax({
    url: "controller.php",
    method: "POST",
    data: { loginData : $("#loginForm").serialize(), 'action':'login'},
    dataType: "json",
    success: function (response) {
    	if(response["success"]==true)
    	{
    		$("#showMessage").html(response['message']);
    		location = '/learningcurve/participant.php?loginid='+response["id"];
    		window.open(location);
    	}else{
    		$("#showMessage").html(response['message']);
    	}
    },
    error: function (request, status, error) {
    	$("#showMessage").html("OOPS! Something Went Wrong Please Try After Sometime!");
    }
    });
    return false;

    我使用的数据类型文本替换为 json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多