小编pac*_*tie的帖子

不能用 async 和 await 做 http 请求

尝试在 nodejs 中使用async和await执行 http 请求,但出错。有任何想法吗?谢谢

got response: 
undefined
/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:539
            callback(parsedData,res);
            ^

TypeError: callback is not a function
    at /home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:539:13
    at Object.parse (/home/tom/learn/node/node_modules/node-rest-client/lib/nrc-parser-manager.js:151:3)
    at ConnectManager.handleResponse (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:538:32)
    at ConnectManager.handleEnd (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:531:18)
    at IncomingMessage.<anonymous> (/home/tom/learn/node/node_modules/node-rest-client/lib/node-rest-client.js:678:34)
    at emitNone (events.js:110:20)
    at IncomingMessage.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1059:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
Run Code Online (Sandbox Code Playgroud)

这是脚本的源代码

var Client = require('node-rest-client').Client;
var client = new Client();
async function test1() {
    response = await client.get("http://localhost/tmp.txt");
    console.log("got response: ");
    console.log(response.headers);
};
test1();
Run Code Online (Sandbox Code Playgroud)

nodejs 的版本是 v8.4.0,在 ubuntu 14.04 上。

asynchronous node.js

3
推荐指数
1
解决办法
4541
查看次数

编码json,数值周围没有引号

我有一个 perl 代码片段

use JSON::XS;
$a = {"john" => "123", "mary" => "456"};
print encode_json($a),"\n";
Run Code Online (Sandbox Code Playgroud)

输出是

{"john":"123","mary":"456"}
Run Code Online (Sandbox Code Playgroud)

想知道是否有一个选项可以让 encode_json 函数(来自 JSON::XS 模块)对其进行编码,以便值 (123, 456) 不被双引号包围。即,喜欢

{"john":123,"mary":456}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法更改 $a 中的哈希值,因为它是从另一个函数传递给我的。想知道 encode_json() 是否有任何技巧。

谢谢!

perl json

2
推荐指数
1
解决办法
1024
查看次数

尝试运行简单的mojolicious程序时出错

Mojolicious的新手.我有两个文件,test1.pl和test2.pm,尝试在我的PC上运行它时出错(Ubuntu 12.04).

$ morbo test1.pl 
Couldn't load application from file "test1.pl": Can't call method "render" on an undefined value at test2.pm line 11.
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢.

这是两个文件

######### test1.pl
use Mojolicious::Lite;
no warnings;
use test2;

get '/' => test2::sendMainPage;
Run Code Online (Sandbox Code Playgroud) ######### test2.pm
package test2;
use Mojolicious::Lite;
use URI::Escape;
use HTML::Entities;
use Data::Dumper;
use JSON;
use Exporter 'import';

sub sendMainPage {
    my $self  = shift;
    $self->render(text => q|<html><body>
<h1>Welcome to test demo page</h1>
</body></html>|);
}


1;
Run Code Online (Sandbox Code Playgroud)

perl mojolicious

1
推荐指数
1
解决办法
139
查看次数

当我有dataType:"JSON"时,为什么我的Ajax请求发送"Content-Type:application/x-www-form-urlencoded"?

当我使用以下内容响应按钮单击时,它调用它(通过使用console.log()验证),但是,它生成的http请求具有标题"Content-Type:application/x-www-form -urlencoded; charset = UTF-8\r \n".不应该是json吗?

我在Ubuntu上使用谷歌浏览器34.0.1847.132.Jquery版本1.8.3.

提前致谢!

function action (mode) {
    console.log("action called with mode " + mode);
    $.ajax({
      type: "POST",
      url: '/saas.php',
      data: {
          action: (mode == 1)? "start" : "stop"
      },
      dataType: "json",
      success: function(data) {
          //alert(data);
          if (data.msg != null) {
              alert(data.msg);
          } else {
            if (mode == 1) {
                document.getElementById('createLoadGen').innerHTML = 'creating loadGen...';
                setTimeout(checkStatus, 1000);
            }
          }
      }
    });
    if (mode == 2) {
        document.getElementById('createLoadGen').innerHTML = '<button onclick="action(1)" >create LoadGen</button>';
        document.getElementById('deleteLoadGen').style.display = 'none'
    }
}
Run Code Online (Sandbox Code Playgroud)

html javascript jquery

1
推荐指数
1
解决办法
1万
查看次数

从std :: string类继承时跳过添加构造函数

尝试参数std :: string,以便它支持方法"bool operator ==(int)".我有错误:

$ g++ -std=c++11 te2.cc
te2.cc: In function ‘int main(int, char**)’:
te2.cc:20:20: error: no matching function for call to ‘mstring::mstring(const char [4])’
te2.cc:20:20: note: candidates are:
te2.cc:10:7: note: mstring::mstring()
te2.cc:10:7: note:   candidate expects 0 arguments, 1 provided
te2.cc:10:7: note: mstring::mstring(const mstring&)
te2.cc:10:7: note:   no known conversion for argument 1 from ‘const char [4]’ to ‘const mstring&’
te2.cc:10:7: note: mstring::mstring(mstring&&)
te2.cc:10:7: note:   no known conversion for argument 1 from ‘const char [4]’ to ‘mstring&&’
Run Code Online (Sandbox Code Playgroud)

这是简单的来源:

#include <unordered_map> …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

1
推荐指数
1
解决办法
75
查看次数

在结构中初始化一个字符串对象

我有一个看起来像这样的结构:

typedef struct {
    int a;
    //other fields
    string s1;
    string s2;
} strMyStruct;
Run Code Online (Sandbox Code Playgroud)

在下面的代码中,append()方法将崩溃,因为x1.s1未构造字符串对象.

strMyStruct x1;
memset(&x1, 0, sizeof(x1));
x1.a = 100
x1.s1.append("....");
Run Code Online (Sandbox Code Playgroud)

我可以做新的放置来解决它new (x1.s1) string;,但它太麻烦了,我没有完全控制结构strMyStruct:有人可能会添加另一个字符串作为一个字段到这个结构并开始使用它并遇到崩溃问题,直到他记得做新的招数.

真的希望字符串类可以在初始化完成后正确处理它memset(&x1, 0, sizeof(x1));.这有诀窍吗?

c++ string

1
推荐指数
1
解决办法
3200
查看次数

从字符串末尾删除一些字符的更有效方法

我想从字符串中删除最后n个字符.我知道有一个方法pop_back()可以删除最后一个字符.我可以在如下的循环中使用它,但它感觉效率不高.

string st("hello world");
for (i=0; i<n; i++) {
    st.pop_back();
}
Run Code Online (Sandbox Code Playgroud)

不知道是否有更有效的替代方案.谢谢.

c++ string

1
推荐指数
1
解决办法
1098
查看次数

C++ placement new持续给出编译错误

尝试使用新的位置,但它一直给我错误.我记得不久前,它正在发挥作用.关于Ubuntu 14.04的g ++(ver 4.8.4).

#include <stdio.h>
typedef unsigned int uint;
struct strSession {
    uint   sessionId;
    uint   srcIp;
    uint   dstIp;
};

int main(int argc, char *argv[]) {
    char buf[20];
    strSession *q = (strSession*)&buf[0]; 
    new (q) strSession;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

得到了错误

$ g++ -std=c++11 te.cc `pkg-config --cflags glib-2.0`
te.cc: In function ‘int main(int, char**)’:
te.cc:12:10: error: no matching function for call to ‘operator new(sizetype, strSession*&)’
  new (q) strSession;
          ^
te.cc:12:10: note: candidate is:
<built-in>:0:0: note: void* operator new(long unsigned int)
<built-in>:0:0: note: …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
713
查看次数

在没有警告的情况下将"void*"转换为int

我需要将"void*"转换为int,但编译器会一直给我警告.不知道是否有办法改变代码,以便编译器不会抱怨.这在代码库中发生了很多,特别是在传递参数以启动新线程时.

$ g++ -fpermissive te1.cc
te1.cc: In function ‘void dummy(void*)’:
te1.cc:4:15: warning: cast from ‘void*’ to ‘int’ loses precision [-fpermissive]
  int x = (int)p;
               ^
Run Code Online (Sandbox Code Playgroud)

这是简单的代码"te1.cc":

#include <stdio.h>

extern void someFunc(int);
void dummy(int type, void *p) {
    if (type == 0) {
        int x = (int)p;
        someFunc(x);
    } else if (type == 1) {
        printf("%s\n", (char*)p);
    }
}

int main(int argc, char *argv[]) {
    void *p = (void*)5;
    dummy(p);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

UDPATE1

我明白我会失去精确度.它有时是有意的.我需要的是有办法在我知道的地方删除警告,确保它是安全的.很抱歉没有在之前说清楚.

UDPATE2

更新了代码片段,以便说明这一点不那么重要.该参数需要传递不同类型的值.我需要一种方法来施放而不产生警告.

c++

1
推荐指数
2
解决办法
4861
查看次数

perl中的非阻塞waitpid()

我试图做非阻塞waitpid,根据perl手册页,waitpid($pid, WNOHANG);会做.但以下似乎是封锁.printf的结果将需要2秒才能显示出来.

$pid = fork;
if (!$pid) {
    exec("sleep 2");
}
waitpid($pid,  WNOHANG);
$retCode = $?;
printf "%04x\n", $retCode;
Run Code Online (Sandbox Code Playgroud)

perl

1
推荐指数
1
解决办法
280
查看次数

标签 统计

c++ ×5

perl ×3

string ×2

asynchronous ×1

c++11 ×1

html ×1

javascript ×1

jquery ×1

json ×1

mojolicious ×1

node.js ×1