Home  >  Q&A  >  body text

angular.js - angularjs get、post、jsonp怎么实现跨域请求?请求数据错误的问题

使用get、post

使用jsonp


在URL加callback也并不能解决问题、
我看了几种方法都是通过服务器端设置,有没有不用同过服务端的方法实现?

PHPzPHPz2715 days ago762

reply all(2)I'll reply

  • 阿神

    阿神2017-05-15 17:03:56

    Cross-domain requests are like going to someone else's house to play. If they don't open the door for you, how can you enter their house. Just let the server set up a whitelist and specify those domain names that can be accessed by requests. La. You can also set the backend to accept requests under any domain name during the testing phase, and then turn off this setting after it is officially launched, but this method is not recommended.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-15 17:03:56

    First, jsonp只能用于getdata.

    Use jsonp跨域获取数据的一个大前提是后端返回的数据得是一段可执行的Javascriptscript, take your code above as an example, the returned content should look like this:

    Request Code

    $http.jsonp('http://192.168.3.248:9000/system/user?callback=parseResponse');

    Return data

    parseResponse({ "dataList": [ { "email": "1646123897@qq.com", "enable": 0, "id": "123094" ...}... ] })

    Otherwise I will report your mistake:Uncaught SyntaxError: Unexpected token :

    Regarding cross-domain, you should first understand what the problem is, and then build public APIs and CORS after reading this

    reply
    0
  • Cancelreply