我正在使用这段代码来获取JSON数据: -
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);
}
});}
这是我得到的json数据
{"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}]}
我在这里检查了这个JSON数据
这是一个有效的JSON数据。但我的电话仍然会出错。获得200状态的警报和未捕获的SyntaxError:意外的令牌:在控制台中。请告诉我这里我做错了什么。
更新: -
答案 0 :(得分:1)
解决错误
请求时没有'Access-Control-Allow-Origin'标头 资源。
您应该在服务器中添加一些代码。 Java版本:
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.
// ...
}
您可以使用dataType: 'json'