CSRF令牌是“未定义的”

时间:2014-11-21 16:47:59

标签: javascript android jquery ajax csrf

我正在使用Android WebView来调用JavaScript函数。基本上,我在按钮的onClickListener中调用JavaScript函数。该函数被调用。在该函数中,我正在尝试向远程服务器发送POST请求。 POST请求是一种注册形式。但是,我无法在JavaScript代码中设置csrf令牌。我当然在我的清单文件中设置了INTERNET PERMISSION。 console.log(csrftoken)给出“未定义”,因此我无法将其设置为我的ajax调用中的标题。这是我的代码:

<!DOCTYPE html>
<html>

               

<body>
<h1>MyHTML</h1>


<script src="jquery.min.js"></script>
<script src="jquery.cookie.js"></script>
<script language="javascript">


 function onClickSignUp(username,email,password) {
            console.log("trying to signup");
            var signup_url = 'http://ec2-54-165-136-178.compute-1.amazonaws.com/accounts/signup/';

            var html = "You have been registered! Thank You";


            var csrftoken = $.cookie('csrftoken');
            console.log("csrf token");
            console.log(csrftoken);
            $.ajaxSetup({
                beforeSend : function(xhr, settings) {
                    if (!this.crossDomain) {
                        xhr.setRequestHeader("X-CSRFToken", csrftoken);
                    }
                }
            });
            $.ajax({
                type : "POST",
                url : signup_url,
                data : {
                    "email" : email,
                    "fullname" : username,
                    "password1" : password,
                    "password2" : password
                },
                dataType : "json",
                success : function(json) {
                    console.log("success");
                    updateView("big-signup-signin-viewer", json, html);
                },
                error : function(xhr, status, errorThrown) {
                    // console.log("Error: " + errorThrown);
                    // console.log("Status: " + status);
                    // console.dir(xhr);
                    // console.log(xhr.responseJSON.form_errors.email[0]);
                    //var html_text = xhr.responseJSON.form_errors.__all__[0] + "<a href='/accounts/password/reset/' ><p>Forgot Password?</p></a>"
                    $("#signup-error").html(xhr.responseJSON.form_errors.email[0]);
                },
                // code to run regardless of success or failure
                complete : function(xhr, status) {

                }
            });

            function updateView(id, json, newview) {
                console.log("updating with new view");
                console.dir(json);
                //document.getElementById("landing_page").outerHtml = newview;
                //$("#landing_page").html("you have signed up");
                // notify user that she/he has been signed up and ask to confirm email
                window.location.replace("/posts/");
            }

            return false;
        };

这是logcat: 这是logcat:

11-21 23:57:30.834:I / chromium(17610):[INFO:CONSOLE(25)]“csrf token”,source:file:///android_asset/mypage.html(25) 11-21 23:57:30.834:I / chromium(17610):[INFO:CONSOLE(26)]“undefined”,source:file:///android_asset/mypage.html(26) 11-21 23:57:30.843:D / audio_hw_primary(183):out_set_parameters:enter:usecase(1:low-latency-playback)kvpairs:routing = 2 11-21 23:57:30.854:D / audio_hw_primary(183):select_devices:out_snd_device(2:speaker)in_snd_device(0:none) 11-21 23:57:30.854:D / msm8974_platform(183):platform_send_audio_calibration:为snd_device发送音频校准(2)acdb_id(15) 11-21 23:57:30.854:D / audio_hw_primary(183):enable_snd_device:snd_device(2:speaker) 11-21 23:57:30.859:D / audio_hw_primary(183):enable_audio_route:apply and update mixer path:low-latency-playback 11-21 23:57:31.362:I / chromium(17610):[INFO:CONSOLE(0)]“XMLHttpRequest无法加载http://ec2-54-165-136-178.compute-1.amazonaws.com/accounts/signup/。请求时没有'Access-Control-Allow-Origin'标题因此,源'null'不允许访问。“,source:file:///android_asset/mypage.html(0) 11-21 23:57:31.366:I / chromium(17610):[INFO:CONSOLE(54)]“未捕获的TypeError:无法读取未定义的属性'form_errors'”,来源:file:///android_asset/mypage.html (54) 11-21 23:57:34.020:D / audio_hw_primary(183):disable_audio_route:reset和update mixer path:low-latency-playback 11-21 23:57:34.021:D / audio_hw_primary(183):disable_snd_device:snd_device(2:speaker)

0 个答案:

没有答案