简体   繁体   中英

Phonegap/Android error: Uncaught ReferenceError: url is not defined at file:///android_asset/www/js/login.js:17

I created a phonegap app for Android and i put the login "logic" into a javascript file. Somethimes the login function works, sometimes it doesn't. When it doesn't, i get the following error:

10-24 10:01:13.211: Web Console(25080): Uncaught ReferenceError: url is not defined at file:///android_asset/www/js/login.js:17

Everything is there and when i used cordova2.0.0 it was working fine, but i had to switch to cordova 2.1.0. Since the switch it doesn't always work fine. Here's how i include the resources in the html file:

<html>

    <head>
        <title>Hello World</title>

        <script type="text/javascript" charset="utf-8" src="js/cordova-2.1.0.js"></script>

        <link rel="stylesheet" href="css/jquery.mobile-1.1.1.min.css" />
        <script type="text/javascript" src="js/jquery-1.8.1.min.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.1.1.min.js"></script>
        <script type="text/javascript" charset="utf-8" src="js/login.js"></script>    

    </head>
...

here's my login.js

$('#page_login_submit').live('click',function(){



  var name = $('#page_login_name').val();
  if (!name) { alert('Please enter your user name.'); return false; }
  var pass = $('#page_login_pass').val();
  if (!pass) { alert('Please enter your password.'); return false; }


  $.ajax({
      url: "http://scoreboard.pronovix.net/?q=scoreboard/user/login.json",
      type: "POST",
      data: 'username=' + encodeURIComponent(name) + '&password=' + encodeURIComponent(pass),
      dataType: "json",
      error: function(jqXHR, textStatus, errorThrown) {
        alert('Login fail: ' + url + '+' + data); //that's line 17
        console.log(JSON.stringify(jqXHR));
        console.log(JSON.stringify(textStatus));
        console.log(JSON.stringify(errorThrown));
      },
      success: function (data) {
        window.location.href = 'index.html';
      }
  });

  return false;
});

Can anyone tell the reason why?

Any help is very much appreciated!

Sincerely,

Zoli

You are trying to reference the url variable to print out in your error but you have not defined it anywhere.

Remember the error function does not have access to the parameters of the ajax function.

Possible solution

If you implement the beforeSend function you can save the url then use it later

beforeSend: function (jqXHR, settings) {
    url = settings.url + "?" + settings.data;
}

This solution was taken from Access the URL of an jQuery Ajax Request in the Callback Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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