在本机ios应用程序中使用superagent时出错

时间:2016-08-09 08:44:48

标签: ios node.js react-native

我做了一个本机应用程序,使用nodejs和express为后端,启用了cors并在android上运行但是当我开始为ios移植应用程序并在模拟器iphone 6中运行它时运行正常,但是当它运行时我用过superagent给我这个错误。

Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
    at Request.crossDomainError (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:80241:9)
    at XMLHttpRequest.xhr.onreadystatechange (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:80311:13)
    at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:10000:15)
    at XMLHttpRequest.setReadyState (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26063:6)
    at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25917:6)
    at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26011:52
    at RCTDeviceEventEmitter.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:9233:23)
    at MessageQueue.__callFunction (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7213:34)
    at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7104:8
    at guard (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7050:1)

这就是我如何使用superagent

const request = require('superagent');

const req = request[options.method.toLowerCase()](options.uri)
        .set(merge({}, options.headers, options.json ? {'Content-Type': 'application/json'} : {}))
        .query(options.qs ? options.qs : {})
        .send(options.json || options.form || options.formData)
      ;
      const requestId = {};

      api.activeRequests.add(requestId);
      return new Promise((resolve, reject) => {
        req.end((err, res) => {
          console.log({err, res});
          api.activeRequests.delete(requestId);
          if (err || !res.ok) {
            console.log(res);
            const error = res ? (res.body ? res.body : res) : err;
            reject(error);
          } else {
            resolve(res.body);
          }
        });
      });

需要帮助!

1 个答案:

答案 0 :(得分:0)

这是由于https请求被发布而不是http。因此,截至目前,因为我处于开发者模式,所以允许http到我的特定域。