2012-06-25 49 views
0

我一直在谷歌周圍的互聯網尋找一個(正確)的方式來@Consume在我的服務器上的Web資源中的「應用程序/ json」文件。發佈json到java服務器

我使用glassfish應用服務器,所以它是一個Java資源。

這裏是調用javascvript代碼:

 var url="/MBC/pages/lives/"; 
     var mygetrequest=new ajaxRequest(); 
     mygetrequest.onreadystatechange=function(){ 
     if (mygetrequest.readyState==4){ 
      if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){ 
       var render=""; 

       var JsonIn =JSON.parse(mygetrequest.responseText); 

       if(JsonIn.error==undefined){ 
        render="generic error"; 
        } 
       }else 
        render=mygetrequest.responseText ; 

       document.getElementById(div).innerHTML=render; 

      }else{ 
      render="An error has occured making the request"; 
      } 
     }; 
     var json2Send = "{" + 
       "boss:\""+location.href.substring(location.href.length-5,location.href.length-4)+"\"," ; 
     if(document.newLive.bval.value=='') 
      json2Send+="bands:[],"; 
     else 
      json2Send+="bands:["+document.newLive.bval.value+"],"; 

     json2Send+="data:\""+document.newLive.dateEvent.value+"\"," + 
       "address:{street:\""+document.newLive.street.value+"\"," + 
         "number:\""+document.newLive.number.value+"\"," + 
         "city:\""+document.newLive.city.value+"\"," + 
         "region:\""+document.newLive.region.value+"\"," + 
         "state:\""+document.newLive.state.value+"\"}" + 
       "}"; 
     mygetrequest.open("POST", url, true); 
     mygetrequest.setRequestHeader("Content-type", "application/json"); 
     mygetrequest.send(json2Send); 

其中json2Send是JSON字符串,客戶端必須發送到服務器。

這裏是不是在服務器端代碼:

@POST 
@Path("configLiveBand") 
@Consumes("application/json") 
@Produces("application/json") 
public String liveBandInsert(String jsonIn, @Context HttpServletRequest request) throws ParseException{ 

現在我問你我有什麼爲了讓服務器讀取輸入JSON字符串與JavaScript來做到。 顯然,我上面描述的方式不起作用。服務器返回

HTTP Status 405 - 

type Status report 

message 

descriptionThe specified HTTP method is not allowed for the requested resource(). 

看我的問題在互聯網上,我發現涉及「的BufferedReader」類「的ReadLine()」方法解決方案。我不喜歡這個解決方案。我更喜歡,如果它是一種方式,注入json文件而不是逐行讀取輸入字符串。

任何幫助廣爲接受 感謝

+0

當然,告訴我,如果我必須明確的其他有關的問題,我有我還沒有寫的問題。 –

+0

如果您刪除了@ @ Produces和@ Consumes註解,那麼是否會調用'liveBandInsert()'?您的瀏覽器發送的請求實際上是什麼樣的(請檢查您的瀏覽器的開發者工具)?如果您使用類似'curl'的方式向具有相同樣本數據的網址發佈郵件,它會起作用嗎?如果刪除'@ Consumes'和'@ Produces'並更改爲'@ GET',您可以在瀏覽器中訪問URL嗎?請在問題中回答,評論可能太小:) –

+0

如果我刪除了@ @ Produces和@ Consumes註釋,則服務器返回與前面相同的描述。你知道嗎?問題是'@ Post'註釋。它適用於get,但不適用於'@ POST'。所以,這是一個JavaScript問題。我不正確的發送請求到服務器。讓我發佈整個電話... –

回答

0

很高興我能幫忙。

我仍然建議使用實際的Javascript對象表示法(JSON)而不是字符串連接來構造您的json2Send,例如,像這樣:

// This creates an "empty" JS object, with no properties. 
var json2Send = new Object(); 
var length = location.href.length; 
// Adding a property is as easy as just setting it, it will be created by this. 
json2Send.boss = location.href.substring(length - 5, length - 4); 
if (document.newLive.bval.value == '') { 
    json2Send.bands = []; 
} else { 
    json2Send.bands = [document.newLive.bval.value]; 
} 
json2Send.data = document.newLive.dateEvent.value; 
// Properties can also be other objects, here created with the 
// object literal notation of { key:value, ...}. 
json2Send.address = { 
    // Inside, it's just another JS object again, 
    // this time setting properties with literal notation key:value 
    // Note how there's no need to quote anything here! 
    street: document.newLive.street.value, 
    number: document.newLive.number.value, 
    city: document.newLive.city.value, 
    region: document.newLive.region.value, 
    state: document.newLive.state.value 
}; 

,然後將其轉換爲一個字符串像這樣的HTTP POST:

mygetrequest.open("POST", url, true); 
mygetrequest.setRequestHeader("Content-type", "application/json"); 
mygetrequest.send(JSON.stringify(json2Send)); 

這將捕獲的語法錯誤要早得多,可減輕您手動引用所有不同的點點滴滴,最有可能更快,它確實使整個事情更加強大。

+0

太乾淨了,謝謝謝謝謝謝:-) –

0

的問題是JSON作爲菲利普寫道:

json2Send+="\"data\":\""+document.newLive.dateEvent.value+"\"," + 
     "\"address\":{\"street\":\""+document.newLive.street.value+"\"," + 
       "\"number\":\""+document.newLive.number.value+"\"," + 
       "\"city\":\""+document.newLive.city.value+"\"," + 
       "\"region\":\""+document.newLive.region.value+"\"," + 
       "\"state\":\""+document.newLive.state.value+"\"}" + 
     "}"; 

,而不是

json2Send+="data:\""+document.newLive.dateEvent.value+"\"," + 
     "address:{street:\""+document.newLive.street.value+"\"," + 
       "number:\""+document.newLive.number.value+"\"," + 
       "city:\""+document.newLive.city.value+"\"," + 
       "region:\""+document.newLive.region.value+"\"," + 
       "state:\""+document.newLive.state.value+"\"}" + 
     "}"; 

現在我可以閱讀,發佈和發送JSON結果返回給客戶端。 ;-)謝謝菲利普

相關問題