请求参数节点JS 500错误

时间:2018-08-19 04:17:03

标签: javascript node.js ecmascript-6

由于uri的生成符合预期,列表数据显示在页面中,但是在请求方法中发送req时,发生500错误而不是重发正文。

uri:http://yufluyuinnepal.com/?vTRIPTYPE=O&vOPSID=O&vSTRFROM=KTM&vSTRFROMTXT=&vSTRTO=PKR&vSTRTOTXT=&vFLIGHTDATE=27-Nov-2018&vRETURNDATE=27-Nov-2018&vADULT=1&vCHILD=0&vNATIONALITY=NP&vNATIONALITYTXT=Nepal&

const uri = `http://yufluyuinnepal.com/?${queryString(query)}`;
console.log(uri);
const req = {
  uri: uri,

};
request(req, (error, response, body) => {
  if (error) {
    return reject(error);
  }

  if (response.statusCode !== 200) {
    return reject(new Error(`Expected 200 but got ${response.statusCode}`));
  }

  return resolve(body);
});

让我知道如何返回尸体以及代码中的错误所在。

3 个答案:

答案 0 :(得分:0)

在“请求npm”模块中,指定请求的类型(GET / POST等)

// Example GET Request

var options = {
      method: "GET",
      url:
        uri,
      headers:
          {
            // headers as per documentation
          }
    };
    
request(options, (error, response, body) => {
  if(error){}
  if(response.statusCode !== 200){}
  return resolve(body);
})

答案 1 :(得分:0)

这是您当前使用的回调函数实现。

public class ServiceLayerRegistery : Registry
{
    public ServiceLayerRegistery()
    {
        Scan(scanner =>
        {
            scanner.TheCallingAssembly();
            scanner.WithDefaultConventions();
            scanner.AssemblyContainingType<myService>();
        });

    }
}

使用const req = { uri: uri, method: 'GET'/'POST' }; request(req, (error, response, body) => { if (error) { console.log(error); } if (response.statusCode !== 200) { //Do something } console.log(body); //Do something }); 模块时,您应该编写如下内容

request-promise

答案 2 :(得分:-1)

请尝试这个

let requestp=require('request-promise');

var options = {

    method: 'POST',

    url: 'uri',

    resolveWithFullResponse: true,

    headers: {

                'Accept': 'application/json',

                'Content-Type' : 'application/json'

            },

            body: TextedValue

        };

     

        await  requestp(options).then(async function(Content){

           await requestp(options).then(async function(response){

                if (await response.statusCode == 200)

                    {

                        console.log(Content); // in ur case it is body

                    }

                 else

                    {

                        console.log("Response code "+response.statusCode+" .Try Again Later")

                   }

                })

           })