-1

I am using this piece of code to get JSON data:-

 function getData(){
            $.ajax({
            type:"GET",
            dataType: "jsonp", 
            url: "http://localhost:8080/SpringFileUpload/service
/api/search?name=kop&query=java&smonth=0&emonth=200&key=7546323574194176&
zipcode=100001", 
            success: function(data) {
                    $("body").append(data);
                }, 
            error: function(jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.status);
                }
        });}

This is my json data which i am getting

{"took":"36","totalHits":"1","hits":[{"index":"kop","type":"doc","id":"L-elA3tKQYShys0Tv7WamA","experience":"86","providedName":"My name","providedPhoneNumber":"+96384545","providedZipCode":"100001","uploadon":"29/07/15","source":null,"url":"file://./Profile1_Java_SOAPUI_JUNIT 15.doc","highlights":[" TestNG. Experience with scripting using Java, Groovy etc\nExtensively used SoapUI to test Web",": Jira, MantisBT, QC\nOperating Systems: Windows XP/2000, DOS\nProgramming Languages: Java, C and C"," automated test plan for the application using Jbehave , Selenium WebDriver and Java.\n\nPerformed GUI Testing","\n\n Environment: Selenium WebDriver, Jbehave, Java, JUnit , Firebug, Jira, MySQL, Eclipse, QC,Window 7"," using Java and Selenium Web driver.\n\nUsed Jira for Defect tracking and triaging.\nUse Maven for build"],"title":"Professional Objective","contentType":null}]}

I checked this JSON data here

http://json.parser.online.fr/

It's a valid JSON data. But still my call is going to error. getting alert with 200 status and Uncaught SyntaxError: Unexpected token : in console. Please tell me what i am doing wrong here.

UPDATE:-

JSFIDDLE

Renu Thakur
  • 551
  • 11
  • 35

1 Answers1

1

To solve the error

No 'Access-Control-Allow-Origin' header is present on the requested resource.

you should add some codes in your server. Java version:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    // ...
    response.addHeader("Access-Control-Allow-Origin", "*");
    // you can change * for your spec url. 
    // if * is used, any url can be access to the data.

    // ...
}

and you can use dataType: 'json'

iplus26
  • 2,518
  • 15
  • 26