在这种情况下,在服务器端有一些带有restApi.js
功能的存档REST
。我的REST函数运行正常,我使用Prompt Command进行测试。
在我的客户端有一些存档index.ejs
,我想用这个文件调用。
我的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);
});
}
我的index.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>
我试过看其他的例子,但是不行(Ajax)。 我需要知道如何解决这个问题,如果有其他最佳实践,请告诉我。
在我的控制台(Chrome)中显示我是否调用了ajax函数:
SyntaxError:位于2位的JSON中的意外标记在JSON.parse(&lt; anonymous&gt;)上解析(C:\ xxxxxxxxxxxxxxxxxxxxxxxxx \ node_modules \ body-parser \ lib \ types \ json .js:88:17)
在C:\ xxxxxxxxxxxxxxxxxxxxxxxxx \ node_modules \ body-parser \ lib \ read.js:116:18
如果我点击(BAD请求)显示: Obs。:与app.js相同的错误,但app.js工作正常。
Cannot GET /restApi.js
如果文件restApi.js是索引后面的文件夹。
文件夹:
Obs。:公用文件夹有index.ejs
答案 0 :(得分:1)
根据错误,我认为您通过AJAX发布的数据语法不正确。
更改function 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);
}
});
}
这可以解决您所面临的错误。
答案 1 :(得分:1)
立即试试......
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.
},
});
}
答案 2 :(得分:1)
答案 3 :(得分:0)
您的网址未指向js / restapi js。 你在js / restapi js中有什么代码? 如果您的操作页面是app js,则必须将其放入url中。 URL: 'JS / restapi.js',