0

我正在编写一些节点代码,用于模拟 ASP.NET ajax 客户端调用。它是作为对服务器的 http post 请求发出的,我已经设法使用 OS X 工具CocoaRestClient完美地设置了请求标头和正文。使用此工具,服务器可以按预期完美响应。当我尝试使用“请求”模块对 Node.js 做同样的事情时,它失败了:

我的脚本:

var request = require('request');

request.post({
    'uri': 'http://[The Url]/[The Service].asmx/[The Operation]',
    'json': '{"callbackcontextkey":"[the context key]",[The set of json formatted key/value pairs] }',
    'headers': { }
}, function(e, r, body) {
    console.log("Response error: %j", e);
    console.log("Response r: %j", r);
    console.log("Response body: %j", body);
});

当我使用 CocoaRestClient 工具时,我指定的只是 Content-Type (application/json) 参数,然后是代码中指定的请求正文('json' 属性值)。我的代码使服务器端返回:

“有一个错误处理请求。”

我也可以在回复中看到这一点:

“json错误”:“真”

我究竟做错了什么?我考虑使用网络嗅探工具来查看差异...

4

1 回答 1

1

参数需要一个 JavaScript 对象,json并且请求将它转换为您的 JSON 字符串。

尝试从它周围删除引号,如下所示:

var request = require('request');

request.post({
    'uri': 'http://[The Url]/[The Service].asmx/[The Operation]',
    'json': {"callbackcontextkey":"[the context key]",[The set of json formatted key/value pairs] },
    'headers': { }
}, function(e, r, body) {
    console.log("Response error: %j", e);
    console.log("Response r: %j", r);
    console.log("Response body: %j", body);
});

或者,您可以将其保留为字符串并设置body参数,而不是json它可能也会以这种方式工作。更多细节在这里:https ://npmjs.org/package/request

于 2013-03-29T21:13:40.030 回答