2016-07-29 122 views
0

實際上,在不同情況下,這裏有幾個問題。從本地到另一臺服務器的本地主機上的Ajax請求在相同的wifi上

所以我連接到一個RESTful服務器託管相同的WiFi。

我在IntelliJ中打開了一個網頁,它在我的機器上創建了一個localhost服務器,以便它可以啓動我的攝像頭。 (當我嘗試從我的文件目錄中打開網頁時,網絡攝像頭無法啓動)

我試圖做一個像這樣的ajax請求。

$.ajax({ 

      url: 'http://192.xxx.x.xx:xxxx/restful/webapi/enroll', 
      //url: 'http://localhost:63xxx/example-1.0.0.0-samples/', 
      type: 'PUT', 
      contentType: "application/json", 
      processData: false, 
      data: data, 
      dataType: 'json', 
      crossDomain:true, 
      error: function (xhr, status) { 
       if(xhr.status != "201"){ // 201 means it is Created, rather than 200 which means Success 
        $("#statusEnrollmentMsg").text("Fail " + status); 
        console.log(error); 
       }else{ 
        $("#textbox").text("success"); 
       } 
      }, 
      success: function (result) { 
       $("#textbox").text("success"); 
      } 
     });

錯誤代碼

jquery.min.js:4 OPTIONS http://192.xxx.x.xx:xxxx/restful/webapi/enroll 
    send @ jquery.min.js:4 
    ajax @ jquery.min.js:4 
    OnEnroll @ example.js:130 
    onclick @ example.html:54 
    example.html:1 
    XMLHttpRequest cannot load http://192.xxx.x.xx:xxxx/restful/webapi/enroll. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
    Origin 'http://localhost:63xxx' is therefore not allowed access. The response had HTTP status code 404. 

我總是得到錯誤,看起來像這樣。 如果不清楚善意評論,我會做出調整。

跨域已被設置爲true!

我想發送一些json對象到這個服務器。我不確定這是否也是適當的協議。請指教。

回答

0

檢查文檔:http://api.jquery.com/jQuery.ajax/

跨域(默認爲false同域的要求,真正爲 跨域請求)

類型:Boolean

如果要強制跨域請求(如JSONP)在同一個 域中,將crossDomain的值設置爲true。例如,對於 ,可以將服務器端重定向到另一個域。 (版本補充說: 1.5)

你的代碼應該是這樣的

$.ajax({ 

      url: 'http://192.xxx.x.xx:xxxx/restful/webapi/enroll', 
      type: 'PUT', 
      crossDomain: true, 
      contentType: "application/json", 
      processData: false, 
      data: data, 
      dataType: 'json', 
      crossDomain:true, 
      error: function (xhr, status) { 
       if(xhr.status != "201"){ // 201 means it is Created, rather than 200 which means Success 
        $("#statusEnrollmentMsg").text("Fail " + status); 
        console.log(error); 
       }else{ 
        $("#textbox").text("success"); 
       } 
      }, 
      success: function (result) { 
       $("#textbox").text("success"); 
      } 
     }); 
其中跨域已經被設置爲true
+0

!你的代碼和我的區別是什麼?我沒有看到它,對不起! –

+0

Ohh對不起,嘗試使用GET/POST方法,如果它的工作,那麼你必須在AJAX請求中修改標題。 –

+0

對不起,什麼是標題?我嘗試了一個POST請求,我回來了同樣的錯誤 –

相關問題