How to make ajax call cross domain

I have access to the API, which is a JSP file and is in JSON format. I am trying to extract this data from a JSP page in a PHP script and process it and then save it in my MySQL server.

The JSON string is valid on the JSP page that I checked in several JSON formatters and an online validator.

This is my code that I use to get JSON data from the page, but every time my ajax call fails.

$('#button').click(function(e){
var url = 'http://xxxxx:8080/StudentAPI/index.jsp';
$.ajax({
    url : url,
    dataType : 'json',
    success : function(response) {
        alert('Success');
    },
    error : function(request, textStatus, errorThrown) {
        alert(request+textStatus+errorThrown);
    }
});
e.preventDefault();
})

Please help me, and any suggestion to make it better is always welcome.

+5
source share
1 answer

You are making an ajax call to the cross domain. This way it will not work if you try it like a regular ajax call.

One way is similar to

  • "Access-Control-Allow-Origin" "*" , ajax.

  • jquery ajax 'crossDomain' 'true' .

jsonp

, .

UPDATE

w3c, , Java. . Java.

, , ajax, "Access-Control-Allow-Origin", "*" .

+3

All Articles