-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

17
  • 3
    Try dataType: "json", instead of dataType: "jsonp", Commented Jul 31, 2015 at 6:12
  • 1
    @PranavCBalan If i use JSON then getting this error No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access Commented Jul 31, 2015 at 6:13
  • 1
    I all ready give you my JSON data which i am getting in response. It's valid. Thats why i cant understand why its going to error Commented Jul 31, 2015 at 6:16
  • 1
    add crossDomain :true, Commented Jul 31, 2015 at 6:16
  • 1
    @PranavCBalan — Why? The purposes of that is to tell jQuery, when it uses XHR (which it isn't because it is making a JSONP request) not to add the custom headers (like x-requested-with) which it only adds if making a Same Origin request just in case it gets redirected and accidentally makes a complex request to a different origin. It's not meeting either of the conditions where that would make a difference. Commented Jul 31, 2015 at 6:27

1 Answer 1

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'

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. let me try this.
It's working. Working like a charm. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.