2016-12-02 25 views
1
<script type="text/javascript"> 
     function passCheck(){ 
       var pass1 = document.getElementById('password').value; 
       var pass2 = document.getElementById('confirmPassword').value; 
       if(pass1 == pass2 && pass1 != ""){ 
        return true; 
       } 
       else{ 
        alert("Both password inputs do not match. Please retry."); 
        document.getElementById('surveyorForm').reset(); 
        return false; 
       } 
      } 

      function change() { 

      var x = document.getElementById("select").value; 


      (function() { 

      var xmlhttp = new XMLHttpRequest(); 
      var url = "http://somejsondata?someget=" + x ; 

      xmlhttp.onreadystatechange=function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        myFunction(xmlhttp.responseText); 
       } 
      } 
      xmlhttp.open("GET", url, true); 
      xmlhttp.send(); 

      function myFunction(response) { 
       var arr = JSON.parse(response); 
       var i; 
       var out = "<div class='form-group'>" + 
          "<label>City</label>" + 
          "<select class='form-control' name='cityId'>" + 
          "<option disabled selected>Select your option</option>"; 

       for(i = 0; i < arr.length; i++) { 
        out += "<option value='" + 
        arr[i].cityId + 
        "'>" + 
        arr[i].cityName + 
        "</option>"; 
       } 
       out += "</select>" + 
        "</div>"; 
       document.getElementById("city").innerHTML = out; 
      } 
      })(); 

     } 

     (function() { 
      var xmlhttp = new XMLHttpRequest(); 
      var url = "http://somejsondata"; 

      xmlhttp.onreadystatechange=function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        myFunction(xmlhttp.responseText); 
       } 
      } 
      xmlhttp.open("GET", url, true); 
      xmlhttp.send(); 

      function myFunction(response) { 
       var arr = JSON.parse(response); 
       var i; 
       var out = "<div class='form-group'>" + 
          "<label>Province</label>" + 
          "<select class='form-control' id='select' name='provId' onchange='change()'>" + 
          "<option disabled selected>Select your option</option>"; 

       for(i = 0; i < arr.length; i++) { 
        out += "<option value='" + 
        arr[i].provinceId + 
        "'>" + 
        arr[i].provinceName + 
        "</option>"; 
       } 
       out += "</select>" + 
        "</div>"; 
       document.getElementById("province").innerHTML = out; 
      } 


      })(); 

      (function() { 
      var xmlhttp = new XMLHttpRequest(); 
      var url = "http://somejsondata"; 

      xmlhttp.onreadystatechange=function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        myFunction(xmlhttp.responseText); 
       } 
      } 
      xmlhttp.open("GET", url, true); 
      xmlhttp.send(); 

      function myFunction(response) { 
       var arr = JSON.parse(response); 
       var i; 
       var out = "<div class='form-group'>" + 
          "<label>Country</label>" + 
          "<select class='form-control' name='countryId'>" + 
          "<option disabled selected>Select your option</option>"; 

       for(i = 0; i < arr.length; i++) { 
        out += "<option value='" + 
        arr[i].countryId + 
        "'>" + 
        arr[i].countryName + 
        "</option>"; 
       } 
       out += "</select>" + 
        "</div>"; 
       document.getElementById("country").innerHTML = out; 
      } 
      })(); 

     </script> 

我已經把這個腳本正好</body>之前,並檢查它的工作鏈接它產生json數據。但爲什麼當我訪問網站時,它在遷移網站之前沒有像我一樣出現。下拉菜單沒有顯示出來。我有工作的JavaScript,但是當我改變服務器和鏈接它沒有工作

我加了一些錯誤 什麼樣的錯誤是這樣

XMLHttpRequest cannot load http://gpx1.gdn/country/read. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://gpx2.gdn' is therefore not allowed access. 
insertSurveyor:1 XMLHttpRequest cannot load http://gpx1.gdn/province/read. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://gpx2.gdn' is therefore not allowed access. 
firebug-lite.js:11883 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.fetchResource @ firebug-lite.js:11883 
firebug-lite.js:30905 Uncaught TypeError: Cannot read property 'push' of undefined(…) 
+0

請問您是否在瀏覽器控制檯中收到錯誤? –

+0

確保您的網址正確無誤。 – Mairaj

+1

也許你的網站在https和http –

回答

1

如果我對你正在做一個XMLHttpRequest到不同的域。因此,瀏覽器會阻止它,因爲出於安全原因,它通常允許同一來源的請求。當你想做一個跨域請求時,你需要做一些不同的事情。

跨源資源共享(CORS)是W3C規範,允許來自瀏覽器的跨域通信。通過構建XMLHttpRequest對象,CORS允許開發人員使用與同域請求相同的習慣用法。

您可以隨時看到此post以瞭解更多信息。 CORS示例如下。

function createCORSRequest(method, url) { 
    var xhr = new XMLHttpRequest(); 
    if ("withCredentials" in xhr) { 

    // Check if the XMLHttpRequest object has a "withCredentials" property. 
    // "withCredentials" only exists on XMLHTTPRequest2 objects. 
    xhr.open(method, url, true); 

    } else if (typeof XDomainRequest != "undefined") { 

    // Otherwise, check if XDomainRequest. 
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests. 
    xhr = new XDomainRequest(); 
    xhr.open(method, url); 

    } else { 

    // Otherwise, CORS is not supported by the browser. 
    xhr = null; 

    } 
    return xhr; 
} 

var xhr = createCORSRequest('GET', url); 
if (!xhr) { 
    throw new Error('CORS not supported'); 
} 

這個link已經很好地解釋了CORS。

+0

謝謝你,我將執行代碼 – Faisal

+0

@Faisal在你實施後,如果這篇文章有幫助,請接受作爲答案。謝謝 –

+0

我還不知道在哪裏把CORS代碼放在我的代碼裏.. – Faisal

相關問題