Rest Web Service问题中带有JSON的jQuery Ajax POST调用 [英] jQuery Ajax POST call with JSON in Rest Web Service Issue

查看:114
本文介绍了Rest Web Service问题中带有JSON的jQuery Ajax POST调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将页面上的JSON对象发布到Rest WS.但是,当我通过jQuery ajax调用发布json作为输出时,我得到的HTML页面的状态为JSON的" HTTP状态405-不允许方法",该状态是我从Rest Web Service发送的.请参考以下代码段.

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.

我正在使用jquery 1.11.3版本.

I am using jquery 1.11.3 version.

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

jQuery Ajax调用:

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');
         }
    });

请注意,我的其余Web服务正在本地的Tomcat中运行.

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

请找到我的Rest Web Service POST代码.

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;
}

}

我的Maven依赖项是:

    <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详细信息:

请找到我的Ajax请求标头.

Please find my ajax request Header.

请找到我的Ajax响应和响应HTML.

Please find my ajax response and response HTML.

需要您的帮助来解决此问题.

Need your help to resolve this issue.

谢谢.

解决方案:

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

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

我用html页面创建了一个Java Web应用程序,该页面具有ajax调用,而从我的ajax调用中,我正在调用相同域的URL.请转给我的ajax电话.

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
         }
    }); 

现在,我已经使用org.mitre.dsmiley.httpproxy.ProxyServlet进行了相同的域调用映射.请参考我的Web xml.

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>

现在,我的ajax正在调用此代理Servlet,并将其重定向到CROS目标Rest Web Service.

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

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

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

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

推荐答案

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

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

我用html页面创建了一个Java Web应用程序,该页面具有ajax调用,而从我的ajax调用中,我正在调用相同域的URL.请转给我的ajax电话.

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
         }
    }); 

现在,我已经使用org.mitre.dsmiley.httpproxy.ProxyServlet进行了相同的域调用映射.请参考我的Web xml.

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>

现在,我的ajax正在调用此代理Servlet,并将其重定向到CROS目标Rest Web Service.

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

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

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

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

这篇关于Rest Web Service问题中带有JSON的jQuery Ajax POST调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆