如何在将响应值传递给另一个函数之前专门修改响应值的第一个索引[0]?

时间:2017-11-03 14:07:26

标签: javascript jquery ajax httpresponse

我目前有这个ajax请求,它运行一个嵌入Tableau Views的函数。然后,我插入安全令牌,以便用户访问Tableau Reports,而无需在此站点上再次登录 Tableau

我的过程是,当我收到来自数据库的回复时,我会注入安全令牌,然后我会查找关键字' ticket'向其添加安全令牌。如果response[0]没有包含关键字' ticket',这意味着我需要在传递给下一个函数之前将该关键字添加到response[0],这样才能完美运行。

以下是我的尝试:response[0].TReports[0].url.replace('server/views','server/trusted/ticket/views');

但是当下一个函数运行时,响应不是已修改的响应。如何接收修改后的回复[0]'?



  $.ajax({
         type: 'POST',
          url: _config.GetHeadersByClientAPI,
          dataType: 'json',
         contentType: 'application/json',
          data: JSON.stringify(data),
          success: function (response) {
          var vizArray = [];
          var items = '';
response[0].TReports[0].url.replace('server/views','server/trusted/ticket/views');   //This is my attempt to edit the value, however it is modifying what it is //being passed to the next forEach. 
    					
                        response.forEach(function (data) {
                            vizArray[data] = data;
    						var trustedticket=ticket;
                            var header = vizArray[data].THeaderTitle;
                            var header_id = vizArray[data].THeaderTitle.replace(" ", "");
                            var reports = vizArray[data].TReports;
    						
                            $('#report-group-title').append("<a href='#" + header_id + "' class='list-group-item d-inline-block collapsed' data-toggle='collapse' data-parent='#sidebar' aria-expanded='false'><i id='h' class='fa fa-book'></i> <span class='d-none d-md-inline'>" + header + " </span></a>   <div class='collapse' id='" + header_id + "'></div >");
                            items += "<a href='#" + header_id + "' class='list-group-item d-inline-block collapsed' data-toggle='collapse' data-parent='#sidebar' aria-expanded='false'><i id='h' class='fa fa-book'></i> <span class='d-none d-md-inline'>" + header + " </span></a>   <div class='collapse' id='" + header_id + "'></div >";
                            var list = '';
                            $.each(reports, function load(j) {
                                vizArray[j] = reports[j]
                                var name = vizArray[j].name;
                                var url = vizArray[j].url.replace("ticket",trustedticket); //Here I inject my security //token
    							
                                var hash = vizArray[j].hash;
                                var vizOptions = {
                                    showTabs: true,
                                    hideToolbar: true
                                };
    							
                                var vizDiv = document.createElement('div');
                                vizDiv.id = hash + 'Viz';
                                document.getElementById('vizes').appendChild(vizDiv);
                                reports.viz = new tableauSoftware.Viz(
                                    document.getElementById(hash + 'Viz'),
                                    url,
                                    vizOptions
                                )
                                console.log(name);
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

String.replace是非变异的。

请考虑以下代码:

var a = "foobar";
a.replace("foo", "bar");
console.log(a); // "foobar"

很容易看出replace没有变异a,而是返回包含&#34; barbar&#34;的操纵字符串。这是你期望替换函数突变a。

相反,你可以这样做:

a = a.replace("foo", "bar");

将变异值分配给。

在你的情况下:

response[0].TReports[0].url = response[0].TReports[0].url.replace('server/views','server/trusted/ticket/views');