angular.js - $http.jsonp request error
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-15 17:14:04
0
2
582

The $http.jsonp request of angularjs reports an error, the code is as follows:

data = {
    a:A,
    b:B,
    c:C,
}
$http.jsonp('url',data).success(function(res){
    console.log(res);
}).error(function(err){
    console.log(err);//控制台打印fasle
})
并报错:Uncaught SyntaxError: Invalid or Unexpected token  url(请求链接)

Click this link, get the data, verify it in json online verification, and find that there is a line break in the B field, but you can't find any line break when you access it in the browser using the link. I contacted the backend, and the backend said that there were no line breaks when entering the database, and my front end couldn’t get the data and couldn’t process it. What else could be the reason for this situation, and how should I solve it?

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(2)
仅有的幸福

Thank you for the invitation. I didn’t see the address. There should be something wrong with the returned data format.
You can open https://www.w3schools.com/js/... in the browser, then use the developer tools to view the Network panel to view the HTTP information.

Example of using XMLHttpRequest object to request data

<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
    <meta charset="UTF-8">
    <title>Angular Repeat-Done Demo</title>
    <script src="https://cdn.bootcss.com/angular.js/1.6.3/angular.min.js"></script>
</head>
<body ng-app="myapp">
<p ng-controller="AppCtrl">
    <h4>Users List</h4>
    <ul>
        <li ng-repeat="member in members">
            <p>
                ID:<span>{{member.id}}</span>
                Name: <span>{{member.login}}</span>
            </p>
        </li>
    </ul>
</p>
<script type="text/javascript">
    var myapp = angular.module("myapp", [])
            .controller("AppCtrl", ['$scope', function ($scope) {
                $scope.getMembers = function () {
                    let MEMBERS_URL = `https://api.github.com/orgs/angular/members?page=1&per_page=5`;
                    let xhr = new XMLHttpRequest();
                    xhr.open("GET", MEMBERS_URL);
                    xhr.onreadystatechange = () => {
                        if (xhr.readyState == 4 && xhr.status == 200) {
                            if (xhr.responseText) {
                                try {
                                    // 手动触发脏值监测
                                    $scope.$apply(function() {
                                        // 在转JSON对象前,对xhr.responseText进行数据格式化
                                        $scope.members = JSON.parse(xhr.responseText);
                                    });
                                } catch (error) {
                                    throw error;
                                }
                            }
                        }
                    };
                    xhr.send(null); // (6)
                };

                $scope.getMembers();
            }])
</script>
</body>
</html>
世界只因有你
$(document).ready(function(){

        $.ajax({
            type: "get",
            async: false,
            url: url,
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"JSON_CALLBACK",
            success: function(json){
                if (json) {
                    try {
                        // 手动触发脏值监测
                        $scope.$apply(function() {
                            console.log(json);
                        });
                    } catch (error) {
                        throw error;
                    }
                }
            },
            error: function(err){
                console.log(err);
            }
        });

    });
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template