Ajax error: SyntaxError: expected expression, got

2019-05-14 14:39发布

问题:

I am trying to login from my server to another server to my other site. But this error comes every time.

SyntaxError: expected expression, got '<'

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E

Please help.

url = "http://www.example.co.uk/email/admin/index.php?Page=&Action=Login";
//        console.log(url);
//        return false;
        postData = {ss_username:"username",ss_password:"password",Action:"login"}
        $.ajax({ //update page and redirect
          type: 'POST',
          url: url,
//          crossDomain: true,
          dataType: "jsonp",
          data: postData,
          success: function (response) {
            console.log(response);

          },
          error: function (response) {
            console.info(response);
          }
        });

回答1:

JSONP is basically wrapping things in a script tag and making a cross-site allowed request (since script tags do not have the same limitations that AJAX does).

Your page returns HTML (I bet the < is part of an HTML tag) which is invalid as a JavaScript object.

Instead, use CORS to perform safe and more sensible cross-site scripting in an allowed and sensible manner.

Note: Always login with HTTPS and not HTTP (otherwise people can MITM you), and JSONP requests (being script tags injected) are always GET.



回答2:

Either your other server encounters an error (so the response is an error html page) or you will have to change the ajax type from 'jsonp' to 'xml'