2016-07-25 102 views
0

REST请求调用REST请求和处理REST响应

POST host:port/chaincode 

    { 
     "jsonrpc": "2.0", 
     "method": "deploy", 
     "params": { 
     "type": 1, 
     "chaincodeID":{ 
      "path":"github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02" 
     }, 
     "ctorMsg": { 
      "function":"init", 
      "args":["a", "1000", "b", "2000"] 
     } 
     }, 
     "id": 1 
    } 

REST响应

{ 
    "jsonrpc": "2.0", 
    "result": { 
     "status": "OK", 
     "message": "52b0d803fc395b5e34d8d4a7cd69fb6aa00099b8fabed83504ac1c5d61a425aca5b3ad3bf96643ea4fdaac132c417c37b00f88fa800de7ece387d008a76d3586" 
    }, 
    "id": 1 
} 

我有这个设置的请求和响应,而他们提到的接口端口是5000。我如何调用这个休息请求并提醒响应结果?我试过jQuery,但我做不到。他们不停地提示我窗口错误。

这是我试着做

$.post('localhost:5000/chaincode', { 
       "jsonrpc": "2.0", 
       "method": "deploy", 
       "params": { 
        "type": 1, 
        "chaincodeID": { 
         "path": "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02" 
        }, 
        "ctorMsg": { 
         "function": "init", 
         "args": ["a", "1000", "b", "2000"] 
        } 
       }, 
       "id": 1 
      }, function (serverResponse) { 

       alert(serverResponse); 
       //do what you want with server response 

      }) 
+0

发布您尝试过的东西。 –

+0

@PaulAbbott新增。 – Alvin

回答

0

你的代码看起来相当不错。

我在发送请求时看到的唯一可能的问题是该服务可能无法在本地主机上运行。

结果是 - 按照规范 - 一个对象。由于alert()只会打印[object],所以最好使用console.log()作为调试输出。

+0

我试过了,他们给我返回一个错误'“加载资源失败:服务器响应状态为404(未找到)chaincode(0,0)”'我如何检查我的localhost:5000正在运行还是不? @joachimschirrmacher – Alvin

+0

链接代码(0,0)表示实际上有一个监听localhost:5000的服务。然而状态404表示找不到一些资源。看来你想使用Hyperledger结构区块链服务,对吧?然后,您的'chainCodeID'字段中的'path'应该包含'chaincode_finished.go'文件的路径。 –

+0

实际上'github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02'不存在。我想你是从某个地方复制它的,但它只是一个例子,你需要适应你自己的需求。 –