search

Home  >  Q&A  >  body text

javascript - Cross-domain, is my jsonp method correct? Why do I keep getting errors?


The path of this ajax request, right?

Is my cross-domain method correct? Why does the console keep reporting errors

高洛峰高洛峰2718 days ago725

reply all(6)I'll reply

  • 迷茫

    迷茫2017-07-05 10:43:03

    Uncaught SyntaxError: Unexpected token: An error like this is caused by the returned json data not being enclosed in "(" and ")" parentheses or not adding a callback value in front.

    reply
    0
  • 黄舟

    黄舟2017-07-05 10:43:03

    There is a problem with the server program, js execution error

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:43:03

    Set the jsonp parameters, and the data returned by the background needs to be wrapped in the jsonp variable you passed
    Front-end:

    $.ajax({
        url: 'xx',
        dataType: 'jsonp',
        jsonp: 'callback',
        ....
    });

    Backend
    callback (returned data)

    reply
    0
  • 高洛峰

    高洛峰2017-07-05 10:43:03

    Add a callback parameter to url: jsoncallback=?, the question mark program will automatically generate the corresponding parameters

    The backend program accepts this parameter and wraps the returned data in this callback function

    eg:
    PHP后端写法
    
    $jsoncallback = $_GET['jsoncallback'];
    
    $result = json_encode($data);
    
    echo  $jsoncallback."(".$result.")";//后端要以这种格式返回数据才能实现跨域
    exit;

    Your return value is not wrapped in a callback function

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:43:03

    jsonp should not be introduced by src in js, and then called callback() in js

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-05 10:43:03

    For jsonp in JQ, do you need to add a callback= to the query string? Field

    reply
    0
  • Cancelreply