从客户端调用服务器端REST函数

时间:2022-03-26 15:04:20

In the case, on the server side have some archive restApi.js with REST functions. My REST functions works fine, i test with Prompt Command.

在本例中,在服务器端有一些archive restApi。js和其他功能。我的REST功能运行良好,我使用提示命令进行测试。

In my client side have some archive index.ejs, And I want to call with this file.

在我的客户端有一些归档索引。ejs,我想调用这个文件。

My restApi.js: Server-side

我的restApi。js:服务器端

var Client = require('./lib/node-rest-client').Client;
var client = new Client();  


var dataLogin = {
   data: { "userName":"xxxxx","password":"xxxxxxxxxx","platform":"xxxx" },
   headers: { "Content-Type": "application/json" }
};

var numberOrigin = 350;

client.registerMethod("postMethod", "xxxxxxxxxxxxxxxxxx/services/login", "POST");

client.methods.postMethod(dataLogin, function (data, response) {
   // parsed response body as js object
   // console.log(data);
   // raw response
    if(Buffer.isBuffer(data)){
      data = data.toString('utf8');
      console.log(data);
      re = /(sessionID: )([^,}]*)/g;
      match = re.exec(data);
      var sessionid = match[2]
      console.log(sessionid);
      openRequest(sessionid, numberOrigin);  // execute fine
    }
});

function openRequest(sessionid, numberOrigin){
  numberOrigin+=1;
   var dataRequest = {
   data: {"sessionID":sessionid,"synchronize":false,"sourceRequest":{"numberOrigin":numberOrigin,"type":"R","description":"Test - DHC","userID":"xxxxxxxxxx","contact":{"name":"Sayuri Mizuguchi","phoneNumber":"xxxxxxxxxx","email":"xxxxxxxxxxxxxxxxxx","department":"IT Bimodal"},"contractID":"1","service":{"code":"504","name":"Deve","category":{"name":"Developers"}}} },
   headers: { "Content-Type": "application/json" }
   };
   client.post("xxxxxxxxxxxxxxxxxxxxxxxxx/services/request/create", dataRequest, function (data, response) {
   // parsed response body as js object
   // console.log(data);
   // raw response
   console.log(data);
   });
}

My index.ejs: Client side

我的指数。ejs:客户端

<html>
<head> ------------- some codes
</head>
<meta ------- /> 
<body>
<script>
function send() {
        $.ajax({
            type: "POST",
            url: "restApi.js",
            data: '{ sendData: "ok" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) { 
            alert("successful!" + result.d);
            }
        });
    }
</script>
<script src="restApi.js"></script>
</body>    
</html>

I've try see others examples but does not work (Ajax). And I need to know how to solved this, if have other Best practice for it, please let me knows.

我已经试着看了其他的例子,但是没有用(Ajax)。我需要知道如何解决这个问题,如果有其他的最佳实践,请告诉我。

In my console (Chrome) show if I call the ajax function: 从客户端调用服务器端REST函数

在我的控制台中(Chrome)显示我是否调用ajax函数:

SyntaxError: Unexpected token s in JSON at position 2
   at JSON.parse (<anonymous>)
   at parse (C:\xxxxxxxxxxxxxxxxxxxxxxxxx\node_modules\body-parser\lib\types\json.js:88:17)
   at C:\xxxxxxxxxxxxxxxxxxxxxxxxx\node_modules\body-parser\lib\read.js:116:18

SyntaxError: JSON中的意外令牌s位于JSON的位置2。解析( ) at parse (C:\ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \node_modules\解析器\解析器\lib型\json.js:88:17

And if I click (BAD Request) show: Obs.: Same error than app.js, but app.js works fine.

如果我点击(坏请求)显示:Obs。:与app.js相同的错误,但是app.js工作正常。

Cannot GET /restApi.js

In the case the file restApi.js Is a folder behind the index.

在这种情况下,文件restApi。js是索引后面的一个文件夹。

Folder:

文件夹:

Obs.: public folder have the index.ejs

奥林匹克广播服务公司。:公用文件夹有index.ejs。

从客户端调用服务器端REST函数

4 个解决方案

#1


1  

Your problem is bad url. In case if you have fiule structure like this you have to point as shown in image 从客户端调用服务器端REST函数

你的问题是url不好。如果你有像这样的纤维结构,你必须像图中那样指向

#2


1  

Based on the error I think the data you are posting via AJAX is not in correct syntax.

基于这个错误,我认为您通过AJAX发布的数据不符合正确的语法。

Change function send() as following.

更改函数send()如下所示。

function send() {
    var obj = { "sendData" : "ok" };
    $.ajax({
        type: "POST",
        url: "restApi.js",
        data: obj,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) { 
        alert("successful!" + result.d);
        }
    });
}

This should resolve the error you are facing.

这将解决您所面临的错误。

#3


1  

Try this now...

现在试试这个…

function send() {
  var obj = { 
    sendData : "ok" 
  };
  $.ajax({
      type: "POST",
      url: "Your url",
      data: obj,
      dataType: "json",
      success: function (result) { 
        alert("successful!" + result.d);
      },
      error: function (error) { 
        console.log("error is",  error); // let us know what error you wil get.
      },
  });
 }

#4


0  

Your url is not pointing to js/restapi js. and what code do you have in js/restapi js? if your action page is app js you have to put it in url. url:'js/restapi.js',

您的url没有指向js/restapi js。在js/restapi js中有哪些代码?如果你的动作页面是app js,你必须把它放到url中。url:“js / restapi.js”,

#1


1  

Your problem is bad url. In case if you have fiule structure like this you have to point as shown in image 从客户端调用服务器端REST函数

你的问题是url不好。如果你有像这样的纤维结构,你必须像图中那样指向

#2


1  

Based on the error I think the data you are posting via AJAX is not in correct syntax.

基于这个错误,我认为您通过AJAX发布的数据不符合正确的语法。

Change function send() as following.

更改函数send()如下所示。

function send() {
    var obj = { "sendData" : "ok" };
    $.ajax({
        type: "POST",
        url: "restApi.js",
        data: obj,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) { 
        alert("successful!" + result.d);
        }
    });
}

This should resolve the error you are facing.

这将解决您所面临的错误。

#3


1  

Try this now...

现在试试这个…

function send() {
  var obj = { 
    sendData : "ok" 
  };
  $.ajax({
      type: "POST",
      url: "Your url",
      data: obj,
      dataType: "json",
      success: function (result) { 
        alert("successful!" + result.d);
      },
      error: function (error) { 
        console.log("error is",  error); // let us know what error you wil get.
      },
  });
 }

#4


0  

Your url is not pointing to js/restapi js. and what code do you have in js/restapi js? if your action page is app js you have to put it in url. url:'js/restapi.js',

您的url没有指向js/restapi js。在js/restapi js中有哪些代码?如果你的动作页面是app js,你必须把它放到url中。url:“js / restapi.js”,