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

jQuery Ajax POST call with JSON in Rest Web Service Issue

我想从我的页面 post JSON object 到 Rest WS。但是当我 posting json 通过 jQuery ajax 调用作为输出时,我得到一个 HTML 页面,其中包含“HTTP 状态 405 - Method Not Allowed" instate of JSON,这是我从 Rest Web 服务发送的。请参考以下代码片段。

我正在使用 jquery 1.11.3 版本。

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

jQuery Ajax 调用:

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

请注意我的休息网络服务是 运行 在我的本地 tomcat。

请找到我的 Rest Web 服务 POST 代码。

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

画外音:

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 请求 Header。

请查找我的ajax回复和回复HTML。

需要您的帮助来解决这个问题。

提前致谢。

解法:

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

我用我的 html 页面创建了一个 java 网络应用程序,该页面有 ajax 调用,从我的 ajax 调用我调用 URL 同域。请参考我的 ajax 电话。

 $.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 完成了相同的域调用映射。请参考我的网站 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 服务。

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

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

数据类型应为 dataType: 'json'

如果您正在使用 contentType: "application/json", ,那么您应该将数据字符串化。

data:JSON.stringify(jsonObj), 

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

我用我的 html 页面创建了一个 java 网络应用程序,该页面有 ajax 调用,从我的 ajax 调用我调用 URL 同域。请参考我的 ajax 电话。

 $.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 完成了相同的域调用映射。请参考我的网站 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 服务。

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

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

尝试使用 ajax 的类型和内容类型设置:

type:'POST',
contentType: 'application/json',

示例:

function loadDoc(){
  var num = '{"empid": 45,"name": "gaurav","salary":566.55}';
  $.ajax({
    url: 'http://localhost:9029/addS',
    method: 'POST',
    type:'POST',
    contentType: 'application/json',
    success: function(response){
      console.log("response::::"+response);
      $("#output").text(response);
    },
    error: function( jqXHR,textStatus, errorThrown){
      console.log("Error askdjk");
      console.log(jqXHR);
      console.log(textStatus);
      console.log(errorThrown);
    }
  });   
}