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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31114503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:57:22  来源:igfitidea点击:

jQuery Ajax POST call with JSON in Rest Web Service Issue

ajaxjsonpostjerseyjax-rs

提问by Suvonkar

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.

我想将 JSON 对象从我的页面发布到 Rest WS。但是,当我通过 jQuery ajax 调用发布 json 作为输出时,我得到了一个 HTML 页面,其中包含“ HTTP 状态 405 - 不允许的方法”的 JSON 状态,这是我从 Rest Web 服务发送的。请参考以下代码片段。

I am using jquery 1.11.3 version.

我正在使用 jquery 1.11.3 版本。

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

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

jQuery Ajax Call:

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

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

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

Please find my Rest Web Service POST code.

请找到我的 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);
}

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:

我的 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 Details:

萤火虫详情:

Please find my ajax request Header.

请找到我的ajax请求标题。

enter image description here

在此处输入图片说明

Please find my ajax response and response HTML. enter image description hereenter image description here

请找到我的 ajax 响应和响应 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.

经过大量的谷歌搜索,我找到了一些解决方案。现在我正在使用 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.

我用我的 html 页面创建了一个 java web 应用程序,它有 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
         }
    }); 

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

现在我已经用 org.mitre.dsmiley.httpproxy.ProxyServlet 完成了同样的域调用映射。请参考我的 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.

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

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

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

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

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

采纳答案by Suvonkar

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

经过大量的谷歌搜索,我找到了一些解决方案。现在我正在使用 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.

我用我的 html 页面创建了一个 java web 应用程序,它有 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
         }
    }); 

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

现在我已经用 org.mitre.dsmiley.httpproxy.ProxyServlet 完成了同样的域调用映射。请参考我的 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.

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

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

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

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

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

回答by Anoop Joshi

datatype should be dataType: 'json'

数据类型应该是 dataType: 'json'

If you are using contentType: "application/json",, then you should stringify your data.

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

data:JSON.stringify(jsonObj), 

回答by Sanyam Jain

Try using type & contentType settings of ajax:

尝试使用 ajax 的 type 和 contentType 设置:

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

Example:

例子:

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