0

I am using Java Restfull Webservice which is dumping Data in postgres SQL. Java WebService function working perfectely.Function is like below

    @Path("/db")
public class DBOperaions {

    @POST
    @Path("/insert")    
    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public String InsertDBData(@PathParam("hname") String hname,
            @PathParam("hdates") String hdates,
            @PathParam("hremark") String hremark,
            @PathParam("isDelete") boolean isDelete,
            @PathParam("created_date") String created_date,
            @PathParam("updated_date") String updated_date) {

  //Data Dumping Code
    }
    }

Now i am trying to access this function from Jquery Ajax like below

var obj = { hname: $("#txtRuleName").val(), hdates: $("#txtRuleDates").val(), hremark: $("#txtRuleRemark").val(), isDelete: false, created_date: 'CURRENT_TIMESTAMP', updated_date: 'CURRENT_TIMESTAMP' };
    $.ajax({
        type: "POST",
        contentType: "application/json",
        dataType: "json",                
        xhrFields: {
            withCredentials: true
        },          
        url: "http://localhost:8015/PostGresTestDB/rest/db/insert",           
        data: JSON.stringify(obj),        
        success: function (data, textStatus, jqXHR) {
            alert('Data Inserted successfully...');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('generateReportFromMR:Error in processing!');
            //alert(jqXHR);
        }
    });

Webservice runs on tomcat server with http://localhost:8015/PostGresTestDB/rest/db/insert and web page running on http://localhost:42229/index.html

I am getting following error

XMLHttpRequest cannot load http://localhost:8015/PostGresTestDB/rest/db/insert. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:49927' is therefore not allowed access.

I tried CORS but no success.

Can Anybody tell me what exactly what i want to do for this?

Jankya
  • 966
  • 2
  • 12
  • 34

2 Answers2

1

You can try using 'jsonp' as the datatype for your request. By using it you would need to specify a callback function in your javascript that will contain the implementation for parsing the json response. The value set in the 'jsonpCallback' option of the $.ajax function must be the same name with the separate callback function that you will implement in your javascript. Instead of writing the code for parsing in the 'success' property of $.ajax, you will do that in the callback function. You also need to modify your Data Dumping Code in your Java source code by first wrapping your JSON response with a string with the same name as the callback function you have specified in 'jsonpCallback' before doing the actual writing of the response.

  • Thanks for Info @Justine.Its working fine but another issue i describe my diffrent question http://stackoverflow.com/questions/28336480/calling-parametrized-function-from-jquery-ajax-process-parameters-value-as-null – Jankya Feb 05 '15 at 05:00
1

As Justin said, jsonp would solve the problem. However, it does not work in all situation because you have to follow the jsonp format. If you have access to the server code,one solution is to configure the server to allow your domain. Set the allow origin to * to allow all origins. This link might be helpful: CORS on Tomcat

Duong Nhu
  • 11
  • 4