2017-05-29 120 views
-1

我有兩個AJAX請求,兩個正在檢查是否沒有獲獎點的新評級可用。ajax請求與url作爲變量不能正常工作

,如果我通過本地主機訪問我的應用程序沒關係:8080IP地址:8080,與映射兩個web服務「/ nextRating」正在提供一個評價,如果有的話。

我創建了兩個全局變量,URL1URL2,一個用於Web服務和一個用於網站,應加載,如果請求中的成功。

函數中的第二個請求正常工作。

第一個不,我只是不明白爲什麼。

function checkForNewRating() 
{ 
    //Extracts the Ip from the url, if accessed via IP:8080/... 
    getIp(); 

    if (ip != null) { 
     url1 = ip.toString() + ":8080/feedback/nextRating"; 
     url2 = ip.toString() + ":8080/feedback/app"; 

     $.ajax({ 
      type: "GET", 
      url: url1, 
      dataType: "json", 
      success: function (data) { 
       console.log(data); 
       window.location.assign(url2); 
      }, 
      error: function() 
      { 
       console.log(window.location.href); 
      } 
     }) 
    } 
    else { 
     $.ajax({ 
      type: "GET", 
      url: "http://localhost:8080/feedback/nextRating", 
      dataType: "json", 
      success: function (data) { 
       console.log(data); 
       window.location.assign("http://localhost:8080/feedback/app"); 
      }, 
      error: function() { 
       console.log(window.location.href); 
      } 
     }) 
    } 
} 

請求的代碼

function getIp() { 
    if (href.includes("192.168.")) { 
     var startIndex = href.indexOf("1"); 
     var endIndex = href.indexOf(":8") 
     ip = href.substring(startIndex, endIndex); 
    } 
} 

此功能工作正常,URL的變量越來越設置正確

+0

什麼是'ip'變量的值?你在哪裏分配它? – karthick

+0

在getIp()開頭 – crunpy

+0

你可以發佈'getIp'代碼嗎? – Hackerman

回答

0

也許你應該返回值

function getIp() { 
    if (href.includes("192.168.")) { 
     var startIndex = href.indexOf("1"); 
     var endIndex = href.indexOf(":8") 
     ip = href.substring(startIndex, endIndex); 
    } 
    return ip; 
} 

而在上述函數獲得值如

function checkForNewRating() 
{ 
    //Extracts the Ip from the url, if accessed via IP:8080/... 
    var ip = getIp();