<small id='uqviY'></small><noframes id='uqviY'>

  • <tfoot id='uqviY'></tfoot>
    1. <legend id='uqviY'><style id='uqviY'><dir id='uqviY'><q id='uqviY'></q></dir></style></legend>
        <bdo id='uqviY'></bdo><ul id='uqviY'></ul>

      1. <i id='uqviY'><tr id='uqviY'><dt id='uqviY'><q id='uqviY'><span id='uqviY'><b id='uqviY'><form id='uqviY'><ins id='uqviY'></ins><ul id='uqviY'></ul><sub id='uqviY'></sub></form><legend id='uqviY'></legend><bdo id='uqviY'><pre id='uqviY'><center id='uqviY'></center></pre></bdo></b><th id='uqviY'></th></span></q></dt></tr></i><div id='uqviY'><tfoot id='uqviY'></tfoot><dl id='uqviY'><fieldset id='uqviY'></fieldset></dl></div>

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

        时间:2023-10-21
            <tbody id='YSlxC'></tbody>
          <tfoot id='YSlxC'></tfoot>

            <i id='YSlxC'><tr id='YSlxC'><dt id='YSlxC'><q id='YSlxC'><span id='YSlxC'><b id='YSlxC'><form id='YSlxC'><ins id='YSlxC'></ins><ul id='YSlxC'></ul><sub id='YSlxC'></sub></form><legend id='YSlxC'></legend><bdo id='YSlxC'><pre id='YSlxC'><center id='YSlxC'></center></pre></bdo></b><th id='YSlxC'></th></span></q></dt></tr></i><div id='YSlxC'><tfoot id='YSlxC'></tfoot><dl id='YSlxC'><fieldset id='YSlxC'></fieldset></dl></div>

              <small id='YSlxC'></small><noframes id='YSlxC'>

              • <bdo id='YSlxC'></bdo><ul id='YSlxC'></ul>
              • <legend id='YSlxC'><style id='YSlxC'><dir id='YSlxC'><q id='YSlxC'></q></dir></style></legend>
                  本文介绍了在 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 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Cocos2D-JS 无法加载从 CocosStudio 导出的 json 文件 下一篇:发送带有附件 Microsoft Graph 的邮件不起作用

                  相关文章

                    <bdo id='8akSG'></bdo><ul id='8akSG'></ul>
                1. <legend id='8akSG'><style id='8akSG'><dir id='8akSG'><q id='8akSG'></q></dir></style></legend>
                    <tfoot id='8akSG'></tfoot>

                  1. <small id='8akSG'></small><noframes id='8akSG'>

                  2. <i id='8akSG'><tr id='8akSG'><dt id='8akSG'><q id='8akSG'><span id='8akSG'><b id='8akSG'><form id='8akSG'><ins id='8akSG'></ins><ul id='8akSG'></ul><sub id='8akSG'></sub></form><legend id='8akSG'></legend><bdo id='8akSG'><pre id='8akSG'><center id='8akSG'></center></pre></bdo></b><th id='8akSG'></th></span></q></dt></tr></i><div id='8akSG'><tfoot id='8akSG'></tfoot><dl id='8akSG'><fieldset id='8akSG'></fieldset></dl></div>