$.ajax and JSONP. ParseError and Uncaught SyntaxError: Unexpected token :

前端 未结 3 962
执笔经年
执笔经年 2020-12-09 04:11

First of all, I\'ve been looking for the answer to my problem in several topics and I couldn\'t find a solution that works with my code.

I\'m trying to get the answe

相关标签:
3条回答
  • 2020-12-09 04:20

    First off you have a typo in your success parameter; you missed the ending s. Also, when you are performing a JSONP request you need to return your JSON information in JSONP format; which should include the callback token as part of the return string. A sample JSONP string would look likes this:

    yourcallbacktoken({"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"})
    

    Take a look at this page for more information on the JSONP specifications: http://devlog.info/2010/03/10/cross-domain-ajax/

    0 讨论(0)
  • 2020-12-09 04:26
    succes : function(data){
    

    That's a typo:

    success : function(data){
    
    0 讨论(0)
  • 2020-12-09 04:31

    If you are using jsonp then the syntax is wrong

    You need to return

    myJsonMethod({"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"});
    

    and also add to your ajax request options

    jsonp: false,
    jsonpCallback: "myJsonMethod"
    

    so

    $.ajax({
        type : "Get",
        url :"http://XXXZZZ/Servlet/Login",
        data :"login="+login+"&password="+pass,
        dataType :"jsonp",
        jsonp: false,
        jsonpCallback: "myJsonMethod",
        success : function(data){
            alert(data);},
        error : function(httpReq,status,exception){
            alert(status+" "+exception);
        }
    });
    

    (and of-course fix the success as @voyager noted)

    0 讨论(0)
提交回复
热议问题