2017-09-12 11 views
0

私はJavaでJSONを使用してソースコードを送信するには?

 //server end 
     response.setContentType("application/json"); 
     JSONObject jsonCode = new JSONObject(); 

     json.put("code", code); //this have a C/C++ code 

     json.put("success", true); 

     //client end 

     function getCode(pointer) 
     { 

      var id = pointer.data.id; 
      var json = { "id" : id }; 
        $.ajax({ 
       "url" : "getCode", 
         "data" : json, 
           "type" : "POST", 

              "success" : function(data) 
       { 

        alert(data.success); //undefined why ? 
        console.log(data); 
        if (!data.success) { 

         alert('code cannot be fetched'); 
        } 
       } 
       , 
        "error" : function(xhr, status, error) 
       { 
        console.log('error'); 
        console.log(xhr.responseText); //this is giving me the JSON ?? 

        editor.setValue(xhr.responseText); 
       } 

JSONオブジェクトを使用してサーブレットからC/C++コードを送信しようとしているしかし、その代わりに、成功に対する機能の、誤差関数が呼び出さなっていますか?

編集を私を助けてください:これは私が

enter code here 
    {"code":#include<iostream> 

    class bbb{ 

    bbb(int array[12][12]=NULL){ 


    } 

    }; 

    class aaa{ 

    }; 
    int func(int=12,int=123); 
    int func(int x,int y){ 
    std::cout<<x<<y<<std::endl; 
    } 


    int main() 
    { 
    int array[12]; 
    int x=12; 

    func(13); 

    if(1==x){ 

    } 
    //add your code here 
    return 0; 
    } 
    ,"success":true} 

を送信しようとしているサンプルJSONで今私はそれをテストするためにRestManをも使用している、とのコードがあるときにJSONが壊れますかのように表示されます誰か助けてもらえますか?

+1

が鳴ります。データが「HTTP 200」で返されていることを確認してください。 – jlaitio

+0

あなたはあなたのログを投稿できますか? –

+0

ステータスが –

答えて

0

コードを含むJSONファイルが無効なJSONです。

有効なJSONファイルでは、文字列値を二重引用符( "value")で囲む必要があります。改行は\ nでエスケープする必要があります。 JSONを使用せず、代わりにコードを送信するか、予約文字をエスケープするJSONエンコーダを使用してください。構文的に有効なJSONファイルは多少のようになります

:サーバーが不正なステータスコードで正しい身体を返しているよう

{ 
    "code":"#include<iostream>\n\nclass bbb{\n\nbbb(int array[12][12]=NULL){\n\n\n}...", 
    "success":true 
} 
関連する問題