如何使用ajax post 请求响应数据进行另一个ajax post 请求?

How to use ajax post request response data to make another ajax post request?

我需要向 api post 发出两个 ajax 请求 url.The 第一个请求 returns 一些数据作为 json (密码,session,sessionId,itemId).

谁能告诉我如何获取密码、会话、sessionId、itemId 值并使用它向同一个 api [=56] 发出另一个 ajax post 请求=] ?

我当前的代码只得到 api 响应,但是如何在第二个 post 请求中使用 password、session、sessionId、itemId 的值?

我试图像这样引用 api 数据 data.keys.password、data.keys.session、data.items.seasonId、data.items.itemId,但我收到错误 data.keys和 data.items 未定义!

谁能告诉我我做错了什么?

(注意:目前我将数据放在第二个 post 请求中,为了演示目的而硬编码)

ajaxpost请求调用:

function callAjax() {


        $.post("https://api-somewebsite.com/process.aspx",
        {
          name: "galaxy",
          itemNum: "123456789"
        },
        function(data,status){
            alert("Data: " + data + "\nStatus: " + status);
            // now i want to use api response data to make another ajax post call
             $.post("https://api-somewebsite.com/process.aspx",
             {
               password: "2342432423ledf",
               session: "23isaofdfjosidfiedfdd=="
               seasonId: "12345",
              itemId:    "334455"
             },
            function(data,status){
            alert("Data: " + data + "\nStatus: " + status);


             });

        });


};

<br>
<button onclick="callAjax()">callAjax</button> 

api 响应(对于第一个 ajax post 请求)我想引用它的项目:

{
     "keys": {
         "password": "2342432423ledf",
         "session": "23isaofdfjosidfiedfdd=="
     },
     "items": {
         "seasonID": 12345,
         "itemID": 334455,
}

}

我认为下面的代码可能对您有所帮助

function callAjax() {


    $.post("https://api-somewebsite.com/process.aspx",
            {
                name: "galaxy",
                itemNum: "123456789"
            },
            function(data,status){
      //return the data as json_encode(yourvalue); from process.aspx 
                console.log(data);
                alert("Data: " + data + "\nStatus: " + status);                   

            });

       };

我们在 chat 中解决了这个问题。

第一个请求未将 Content-Type header 设置为 JSON,因此未解析响应。将 dataType 设置为 json$.post 的最后一个参数)会正确设置 header,响应将被解析为 JSON。这意味着 data 是一个 object,其属性可以被访问,而不是一个字符串。