【问题标题】:Call JSON in Website using jQuery POST Method使用 jQuery POST 方法在网站中调用 JSON
【发布时间】:2017-07-21 07:33:48
【问题描述】:

我已经创建了 Java REST API,所以我想使用 jQuery 在我的网站中调用 JSON(使用 HTML 和 CSS 创建),这是 POST 方法,我的 JSON 是,

{
    "sessionID" : "25574",
    "interactiveChannel" : "CC_INTERACT_TEST",
    "audienceLevel" : "Customer",
    "relyOnExistingSession" : false,
    "debug" : false,
    "audienceID":[
     {
       "name":"CustomerID",
       "valueAsNumeric":"200",
       "valueDataType":"numeric"
     }
   ]  
}

那么,如何使用 jQuery 发布这个 JSON 呢?

【问题讨论】:

    标签: jquery html css json rest


    【解决方案1】:

    使用 $.post 方法。它是如何工作的:

     $.post(url, data, callbackFunction);
    

    例如:

    var data = {
       myExampleProp: myExampleValue
    };
    
    $.post('www.mydomain.com/api/sessions', data, function() {
        // Successfully posted
    });
    

    您也可以使用axios,它使用promises 而不是回调。这是为了避免臭名昭著的回调地狱。它也更容易使用。

    在我的示例中,我使用普通对象,但您也可以发送字符串 (JSON)。

    【讨论】:

    • 我做了,但它显示 415 错误不支持的媒体类型
    • var data = {“sessionID”:“25574”,“interactiveChannel”:“CC_INTERACT_TEST”,“audienceLevel”:“客户”,“relyOnExistingSession”:false,“debug”:false,“audienceID ":[ { "name":"CustomerID", "valueAsNumeric":"200", "valueDataType":"numeric" } ] }; $(document).ready(function(){ $("button").click(function(){ $.post("link", data, function(data,status){ alert("Data: " + data.名称 + "\n状态:" + 状态); }); }); });
    • 以上是我的发帖请求
    【解决方案2】:

    使用ajax方法就可以了

    var data = {"sessionID" : "25574","interactiveChannel":"CC_INTERACT_TEST","audienceLevel" : "Customer","relyOnExistingSession" : false,"debug" : false,"audienceID":[{"name":"CustomerID","valueAsNumeric":"200","valueDataType":"numeric"}]}
    
    var saveData = $.ajax({
          type: 'POST',
          url: "http://127.0.0.1/abc/users",  //change the url to your url
          data: data,
          contentType: "application/json",
          dataType: "json",
          success: function(resultData) { alert(resultData); },
          error:function(resultData){alert("Error Reading url or data");}
    });
    

    在您的 java 中添加消耗(请求媒体类型)和生产(响应媒体类型)

     @RequestMapping(value = {"/users"}, method = RequestMethod.POST,
        consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
        produces = MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE)
    

    【讨论】:

    • @RequestMapping(value = "/startSession", method = RequestMethod.POST) @ResponseBody public Status sendStartSession(@RequestBody StartSession startSession) { System.out.println("Interactctive channel = " + startSession. getInteractiveChannel());返回 interactDemoService.sendStartSessionRequest(startSession); }
    • 以上是我的 Java 代码
    • @sunil 更新了我的答案,请立即检查,不要忘记将 ajax url 更改为您的帖子 url 引用:docs.spring.io/spring/docs/3.2.x/spring-framework-reference/…
    • 检查我的 ajax 代码更新它现在可能工作,请发布你的 java 方法来接收这个发布请求
    • 尝试删除 content-type 行,并请发布您的网址,以便我可以对其进行测试并为您提供准确的结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多