Ajax调用:ReferenceError:未定义url

时间:2018-01-05 05:12:30

标签: javascript jquery angularjs ajax

MY js code:

function OrderFormControllerForRE($scope, $http) {

        var urlString = window.location.href;
        var urlParams = parseURLParams(urlString);

        var jsonData = $.ajax({
            url: "http://******:8989/getRuleEngine",
            dataType: "json",
            crossDomain: true,
        }).responseText;

        debugger;
        if (!jsonData || jsonData === "") {
                window.location.pathname = "../html/noDataFetched.html";
        }

        var parsed = JSON.parse(jsonData);

        if (parsed.error) {
            window.location.pathname = "../html/error.html";
        }

        $scope.ruleEngineJSON = parsed;

        $scope.ruleName = "";
        $scope.priority = "";
        $scope.modifyPriority = function(ruleName) {
                $scope.ruleName = ruleName;
                $scope.priority = $scope.ruleEngineJSON[ruleName];
        };
};

function navigateRuleEngine() {
    window.location.pathname = "../html/modifyRuleEngine.html";
}




function parseURLParams(url) {
    var queryStart = url.indexOf("?") + 1,
        queryEnd = url.indexOf("#") + 1 || url.length + 1,
        query = url.slice(queryStart, queryEnd - 1),
        pairs = query.replace(/\+/g, " ").split("&"),
        parms = {}, i, n, v, nv;

    if (query === url || query === "") return;

    for (i = 0; i < pairs.length; i++) {
        nv = pairs[i].split("=", 2);
        n = decodeURIComponent(nv[0]);
        v = decodeURIComponent(nv[1]);

        if (!parms.hasOwnProperty(n)) parms[n] = [];
        parms[n].push(nv.length === 2 ? v : null);
    }
    return parms;
}

我的Html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>POC-SalesForce</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js"></script>
    <img src="../img/spinner.gif" id="img" style="display:none"/>

    <link href="https://fonts.googleapis.com/css?family=Cookie|Open+Sans:400,700" rel="stylesheet"/>
    <!-- The main CSS file -->
    <link href="../css/style.css" rel="stylesheet"/>
    <link href="../css/codeReview.css" rel="stylesheet"/>
    <link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <link rel="stylesheet prefetch" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.min.css">
    <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/preloader.css">
    <script src="../js/scriptRuleEngine.js"></script>
</head>
<body ng-app ng-controller="OrderFormControllerForRE">
<header>
    <div class="container">
        <div id="branding">
            <h1><span class="highlight">Modify Rule Engine </span></h1>
        </div>

    </div>
</header>

<section class="col-md-12 col-lg-12 dateSection">
    <!--<form method="get">-->
    <table id="ruleEngine" class="table table-sm table-inverse table-responsive table-striped table-bordered"
           cellpadding="20" width="100%">
        <thead>
        <tr>
            <th>Rule Name</th>
            <th>Priority</th>
            <th></th>
        </tr>
        </thead>
        <tfoot>
        <tr ng-repeat="(key,value) in ruleEngineJSON">
            <td>{{key}}</td>
            <td>{{value}}</td>
            <td><button type="button" class="btn btn-default"
                        ng-click="modifyPriority(key)">Modify</button></td>
        </tr>
        </tfoot>
    </table>
    <!--</form>-->

</section>

<footer>
    <p>Salesforce Free Code Review For Syngenta, Copyright &copy; 2017</p>
    <p>Developed By - Nagendra Kumar Singh</p>
</footer>
</body>
</html>

当我加载页面时,url会触发控制器并返回正确的JSON格式,因为我可以通过访问http:// ********:8989 / getRuleEngine

但是当页面加载时,我在调试时在JS中出现了这个错误:

"ReferenceError: url is not defined
    at eval (eval at OrderFormControllerForRE (http://usblrnagesingh1:8989/js/scriptRuleEngine.js:12:9), <anonymous>:1:1)
    at c.OrderFormControllerForRE (http://usblrnagesingh1:8989/js/scriptRuleEngine.js:12:9)
    at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js:35:36)
    at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js:35:165)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js:68:194
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js:54:298
    at r (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js:7:392)
    at K (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js:54:163)
    at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js:47:397)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js:47:17"

enter image description here

我不知道为什么它会失败,因为我有一个类似的页面有<body ng-app ng-controller="OrderFormController">,脚本有函数OrderFormController($ scope,$ http){}。它在那里工作正常,没有任何错误。

更新 我甚至尝试更改角度js版本,但这不起作用。此版本应该可以工作,因为我有类似的页面正在工作。

0 个答案:

没有答案