【问题标题】:Return value from JAVA API Value从 JAVA API 值返回值
【发布时间】:2017-02-09 05:44:49
【问题描述】:

单击按钮后,我通过 API POST 调用将 JSON 发送到 Java 服务器端。在验证 JSON 时,服务器端函数返回一个字符串。

如何在我的 JavaScript 客户端访问该字符串?
该字符串会告诉我 JSON 是否有效。

【问题讨论】:

  • 使用jquery ajax POST,实现成功处理块
  • 服务器端函数返回String是什么意思?它是如何做到的?回到哪里?
  • 请说明您如何发送 POST 请求。我敢打赌,您的 success 函数已经返回了您的响应。阅读该发布功能的文档
  • 函数 xhrPostCall(jsonInput, postUrl){ xhrPost.open("POST",postUrl, true); xhrPost.setRequestHeader("内容类型", "应用程序/json"); xhrPost.onreadystatechange = function() { if(xhrPost.readyState == 4 && xhrPost.status == 200) { msg="环境成功添加!";警报(xhrPost.status+jsonInput+msg); console.log('发布成功:'+jsonInput+msg); } else{msg="环境添加失败!";警报(xhrPost.status+jsonInput+msg); console.log('发帖不成功'+jsonInput+msg); } } xhrPost.send(jsonInput); }

标签: java ajax api return


【解决方案1】:

你可以在jQuery中使用parseJSON(json)函数,例如:

          function getValue(actionName) {   
              $j.ajax({
                 url: actionName + ".do",
                 data: {
                    "action" : "getJsonValue"
                  },
                  dataType: "json",
                  success: function(response) {
                     if (response.errorCode) {
                         showErrorMessage(response.errorMessage);
                         return;
                     }
                     response = $j.parseJSON(response);
                     return response;
                  },
               });
           }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2011-01-19
    • 2011-10-10
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多