在 Rest Web 服务问题中使用 JSON 进行 jQuery Ajax POST 调用

2023-10-21前端开发问题
7

本文介绍了在 Rest Web 服务问题中使用 JSON 进行 jQuery Ajax POST 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想将一个 JSON 对象从我的页面发布到 Rest WS.但是,当我通过 jQuery ajax 调用作为输出发布 json 时,我得到了一个 HTML 页面,其中包含HTTP Status 405 - Method Not Allowed"的 JSON 状态,这是我从 Rest Web Service 发送的.请参考以下代码片段.

我使用的是 jquery 1.11.3 版本.

解决方案

经过大量谷歌搜索,我找到了一些解决方案.现在我正在使用 HTTP-Proxy-Servlet.

我用我的 html 页面创建了一个 java web 应用程序,该页面有 ajax 调用,并且从我的 ajax 调用中我调用了同一个域的 URL.请参考我的 ajax 调用.

 $.ajax({url: "rest/api/crunchifyService/jsonpost",方法:POST",数据:JSON.stringify(jsonObj),数据类型:'json',内容类型:应用程序/json",成功:函数(结果,状态,jqXHR){//做一点事},错误(jqXHR,textStatus,errorThrown){//做一点事}});

现在我已经使用 org.mitre.dsmiley.httpproxy.ProxyServlet 完成了相同的域调用映射.请参考我的 web xml.

<servlet><servlet 名称>代理</servlet 名称><servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class><初始化参数><param-name>targetUri</param-name><param-value><!-- 跨域 URL 到这里--></param-value></初始化参数><初始化参数><参数名称>日志</参数名称><参数值>true</参数值></初始化参数></servlet><servlet 映射><servlet 名称>代理</servlet 名称><url-pattern>/rest/*</url-pattern></servlet 映射>

现在我的 ajax 正在调用这个 Proxy Servlet,它正在重定向到 CROS 目标 Rest Web 服务.

请参考以下网址,您将获得更多详细信息.

https://github.com/mitre/HTTP-Proxy-Servlet

I want to post a JSON object from my page to a Rest WS. But when I am posting json through jQuery ajax call as output I am getting a HTML page with "HTTP Status 405 - Method Not Allowed" instate of JSON, which I am sending from Rest Web Service. Please refer the following code snippet.

I am using jquery 1.11.3 version.

http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js

jQuery Ajax Call:

 $.ajax({
        url: "http://localhost:8080/MyWebService/api/myService/jsonpost",
        method: "POST",
        data: jsonObj,
        dataType: 'application/json',
        contentType: "application/json",
         success: function(result){
              alert(result);
         },
         error(){
             console.log('Error');
         }
    });

Please note my rest web service is running in my local tomcat.

Please find my Rest Web Service POST code.

@POST
@Path("/jsonpost")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String crunchifyJsonPostREST(SampleVO input) throws JSONException{
    SampleVO sampleVO = input;
    return new Gson().toJson(sampleVO);
}

VO:

public class SampleVO {

private String name;

/**
 * @return the name
 */

@XmlElement
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

}

My Maven Dependency is:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.8</version>
    </dependency>

Firebug Details:

Please find my ajax request Header.

Please find my ajax response and response HTML.

Need your help to resolve this issue.

Thanks is advance.

Solution:

After lot of googling I have found some solution. Now I am using HTTP-Proxy-Servlet.

I have crated a java web application with my html page which has the ajax call and from my ajax call I am calling a URL of same domain. Please refer my ajax call.

 $.ajax({
        url: "rest/api/crunchifyService/jsonpost",
        method: "POST",
        data: JSON.stringify(jsonObj),
        dataType: 'json',
        contentType: "application/json",
         success: function(result,status,jqXHR ){
              //Do something
         },
         error(jqXHR, textStatus, errorThrown){
             //Do something
         }
    }); 

Now I have done this same domain call mapping with org.mitre.dsmiley.httpproxy.ProxyServlet. Please refer my web xml.

<servlet>
    <servlet-name>proxy</servlet-name>
    <servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class>
    <init-param>
        <param-name>targetUri</param-name>
        <param-value><!-- Cross domain URL goes here --></param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>proxy</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Now my ajax is calling this Proxy Servlet and it is redirecting to the CROS destination Rest Web Service.

Please refer the following URL, you will get more details.

https://github.com/mitre/HTTP-Proxy-Servlet

解决方案

After lot of googling I have found some solution. Now I am using HTTP-Proxy-Servlet.

I have crated a java web application with my html page which has the ajax call and from my ajax call I am calling a URL of same domain. Please refer my ajax call.

 $.ajax({
        url: "rest/api/crunchifyService/jsonpost",
        method: "POST",
        data: JSON.stringify(jsonObj),
        dataType: 'json',
        contentType: "application/json",
         success: function(result,status,jqXHR ){
              //Do something
         },
         error(jqXHR, textStatus, errorThrown){
             //Do something
         }
    }); 

Now I have done this same domain call mapping with org.mitre.dsmiley.httpproxy.ProxyServlet. Please refer my web xml.

<servlet>
    <servlet-name>proxy</servlet-name>
    <servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class>
    <init-param>
        <param-name>targetUri</param-name>
        <param-value><!-- Cross domain URL goes here --></param-value>
    </init-param>
    <init-param>
        <param-name>log</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>proxy</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Now my ajax is calling this Proxy Servlet and it is redirecting to the CROS destination Rest Web Service.

Please refer the following URL, you will get more details.

https://github.com/mitre/HTTP-Proxy-Servlet

这篇关于在 Rest Web 服务问题中使用 JSON 进行 jQuery Ajax POST 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax
Ajax call in rails 3.2.3 with will_paginate gem(使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax)...
2024-04-20 前端开发问题
6

在某些(不是全部)浏览器中读取 D3 javascript 中的 JSON 文件时出现问题
Problems reading JSON file in D3 javascript in some (not all) browsers(在某些(不是全部)浏览器中读取 D3 javascript 中的 JSON 文件时出现问题)...
2024-04-19 前端开发问题
8

使用视图附加或嵌入节点添加表单
Attach or embed node add form with a view(使用视图附加或嵌入节点添加表单)...
2024-04-19 前端开发问题
9