Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jqueryajaxpostjson-Response_Javascript_Jquery_Json_Ajax_Api - Fatal编程技术网

Javascript jqueryajaxpostjson-Response

Javascript jqueryajaxpostjson-Response,javascript,jquery,json,ajax,api,Javascript,Jquery,Json,Ajax,Api,我需要发送一个API请求并获得一个响应,然后暂时将其作为JSON以html形式写入前端(这是第一步,解析表是第二步)。我在这方面没有受过教育,我已经尝试了我在这里找到的一切,但它总是给我错误“加载资源失败:服务器响应状态为405(不允许使用方法)” 在API Tester上,服务器响应是正确的,所以我假设服务器(我无法控制)设置正确 请求如下所示: POST https://mmb.some-url.com:8082/api/calculators/hc/calculate **Request

我需要发送一个API请求并获得一个响应,然后暂时将其作为JSON以html形式写入前端(这是第一步,解析表是第二步)。我在这方面没有受过教育,我已经尝试了我在这里找到的一切,但它总是给我错误“加载资源失败:服务器响应状态为405(不允许使用方法)”

在API Tester上,服务器响应是正确的,所以我假设服务器(我无法控制)设置正确

请求如下所示:

POST https://mmb.some-url.com:8082/api/calculators/hc/calculate

**Request**
HEADERS
Content-Type:application/json
Authorization:Bearer token-code-here

**BODY**
{
  "product_id": "hc",
  "dimensions": {
    "length": 1111,
    "standard_lengths": "yes"
  },
  "heating": {
    "temp_water_in": 75,
    "temp_water_out": 65,
    "temp_room": 20
  },
  "cooling": {
    "temp_water_in": 7,
    "temp_water_out": 12,
    "temp_room": 27,
    "relative_humidity": 50
  }
}
<button type="button" class="btn">Load</button>
<p class="text">Text replaced by a response</p>

<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>

<script>
  $('.btn').click(function () {

    $('.text').text('Loading...');

    jQuery.support.cors = true;

    $.ajax({
      url: 'https://mmb.some-url.com:8082/api/calculators/hc/calculate',
      type: 'POST',
      headers: {
        'Content-type': 'application/json',
        "Access-Control-Origin": "*",
        'Authorization': 'Bearer token-code-here',
      },
      crossDomain: true,
      dataType: 'jsonp',
      data: JSON.stringify({
        "product_id": "hc",
        "dimensions": {
          "length": 1111,
          "standard_lengths": "yes"
          },
        "heating": {
          "temp_water_in": 75,
          "temp_water_out": 65,
          "temp_room": 20
          },
        "cooling": {
          "temp_water_in": 7,
          "temp_water_out": 12,
          "temp_room": 27,
          "relative_humidity": 50
          }
      }),
      success: function (data) {
        $('.text').text(JSON.stringify(data));
      }});
  });
</script>
答案是一段JSON代码

我的jQuery代码如下所示:

POST https://mmb.some-url.com:8082/api/calculators/hc/calculate

**Request**
HEADERS
Content-Type:application/json
Authorization:Bearer token-code-here

**BODY**
{
  "product_id": "hc",
  "dimensions": {
    "length": 1111,
    "standard_lengths": "yes"
  },
  "heating": {
    "temp_water_in": 75,
    "temp_water_out": 65,
    "temp_room": 20
  },
  "cooling": {
    "temp_water_in": 7,
    "temp_water_out": 12,
    "temp_room": 27,
    "relative_humidity": 50
  }
}
<button type="button" class="btn">Load</button>
<p class="text">Text replaced by a response</p>

<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>

<script>
  $('.btn').click(function () {

    $('.text').text('Loading...');

    jQuery.support.cors = true;

    $.ajax({
      url: 'https://mmb.some-url.com:8082/api/calculators/hc/calculate',
      type: 'POST',
      headers: {
        'Content-type': 'application/json',
        "Access-Control-Origin": "*",
        'Authorization': 'Bearer token-code-here',
      },
      crossDomain: true,
      dataType: 'jsonp',
      data: JSON.stringify({
        "product_id": "hc",
        "dimensions": {
          "length": 1111,
          "standard_lengths": "yes"
          },
        "heating": {
          "temp_water_in": 75,
          "temp_water_out": 65,
          "temp_room": 20
          },
        "cooling": {
          "temp_water_in": 7,
          "temp_water_out": 12,
          "temp_room": 27,
          "relative_humidity": 50
          }
      }),
      success: function (data) {
        $('.text').text(JSON.stringify(data));
      }});
  });
</script>
加载

文本替换为响应

$('.btn')。单击(函数(){ $('.text').text('加载…'); jQuery.support.cors=true; $.ajax({ 网址:'https://mmb.some-url.com:8082/api/calculators/hc/calculate', 键入:“POST”, 标题:{ “内容类型”:“应用程序/json”, “访问控制源”:“*”, “授权”:“此处的承载令牌代码”, }, 跨域:是的, 数据类型:“jsonp”, 数据:JSON.stringify({ “产品id”:“hc”, “尺寸”:{ “长度”:1111, “标准长度”:“是” }, “供暖”:{ “温度水”:75, “临时用水”:65, “临时房间”:20 }, “冷却”:{ “温度水”:7, “临时用水”:12, “临时房间”:27, “相对湿度”:50 } }), 成功:功能(数据){ $('.text').text(JSON.stringify(data)); }}); });
我的代码有什么问题吗?有人能帮我吗


非常感谢您

您的代码在隔离状态下似乎很好。请注意,405错误意味着您正在使用服务器不希望该端点使用的HTTP谓词。在这种情况下,请使用POST。试着去拿,或者放。参考文档我想你不能直接从浏览器调用这样的API,即使启用了CORS。这总是很难实现的。APITester不直接从浏览器执行调用。它将您的指示发送到服务器端(到
https://apitester.com/checks/test
,如果检查网络检查器),则在服务器端执行API调用,然后将结果发送回浏览器以显示。这正是您应该做的。您的API不支持请求后检查API文档,以了解允许使用哪些HTTP谓词。405部署后意味着它已连接,但由于安全问题,您无法访问该方法。您需要调用部署应用的主url,而不是直接访问API。如果你使用的是免费服务器,那就选择heroku吧。它使用起来非常简单方便。你的代码在隔离状态下看起来很好。请注意,405错误意味着您正在使用服务器不希望该端点使用的HTTP谓词。在这种情况下,请使用POST。试着去拿,或者放。参考文档我想你不能直接从浏览器调用这样的API,即使启用了CORS。这总是很难实现的。APITester不直接从浏览器执行调用。它将您的指示发送到服务器端(到
https://apitester.com/checks/test
,如果检查网络检查器),则在服务器端执行API调用,然后将结果发送回浏览器以显示。这正是您应该做的。您的API不支持请求后检查API文档,以了解允许使用哪些HTTP谓词。405部署后意味着它已连接,但由于安全问题,您无法访问该方法。您需要调用部署应用的主url,而不是直接访问API。如果你使用的是免费服务器,那就选择heroku吧。它非常容易使用。