html与c交互 webassembly,c – 是否可以使用WebAssembly提交HTTP请求?

我正在尝试在WebAssembly中提交一个简单的HTTP GET请求.为此,我编写了这个程序(从

Emscripten site复制,稍作修改):

#include

#include

#ifdef __EMSCRIPTEN__

#include

#include

#endif

void downloadSucceeded(emscripten_fetch_t *fetch) {

printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);

// The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];

emscripten_fetch_close(fetch); // Free data associated with the fetch.

}

void downloadFailed(emscripten_fetch_t *fetch) {

printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status);

emscripten_fetch_close(fetch); // Also free data on failure.

}

unsigned int EMSCRIPTEN_KEEPALIVE GetRequest() {

emscripten_fetch_attr_t attr;

emscripten_fetch_attr_init(&attr);

strcpy(attr.requestMethod, "GET");

attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;

attr.onsuccess = downloadSucceeded;

attr.onerror = downloadFailed;

emscripten_fetch(&attr, "http://google.com");

return 1;

}

当我使用$EMSCRIPTEN / emcc main.c编译它时-O1 -s MODULARIZE = 1 -s WASM = 1 -o main.js –emrun -s FETCH = 1我得到错误

ERROR:root:FETCH not yet compatible with wasm (shared.make_fetch_worker is asm.js-specific)

有没有办法从WebAssembly运行HTTP请求?如果是,我该怎么办?

更新1:以下代码尝试发送GET请求,但由于CORS问题而失败.

#include

#include

#ifdef __EMSCRIPTEN__

#include

#include

#endif

unsigned int EMSCRIPTEN_KEEPALIVE GetRequest() {

EM_ASM({

var xhr = new XMLHttpRequest();

xhr.open("GET", "http://google.com");

xhr.send();

});

return 1;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值