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

2022-01-21 00:00:00 json post ajax jax-rs jersey

我想将一个 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

相关文章