I have problem with AJAX call. error with parseerror. my code:
$.ajax({
type: "GET",
url: "http://localhost:8089/SpringNew/tesget",
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
success: function(resp){
alert("Server said123:\n '" + resp );
},
error: function(request, errorText, errorCode){
alert('Error121212: ' + errorText);
}
});
When I use this code, error with alert "Error121212: parseerror", and when I use dataType: "json", I have error: "XMLHttpRequest cannot load".
I created web services with Java Spring, in webservices, I run in port 8089, and in frontend (I use SAPUI5) in port 8080.
my web services:
How to fix this problem?
Thanks.
Bobby
The question seems to have missed some details. I believe this is because of the CORS headers from the server side. You should allow the origin that you are requesting with.
For allowing all, you can set the header 'Access-Control-Allow-Origin' to be '*'
This should solve your issue.
Your datatype contains an typo imho. It should be "json" instead of "jsonp". You may also try ...[url to service]/[entityset]?$format=json
Related
From my work gave me a project to finish with which I am having the following problem:
In solution there is web API and RESTful API in MVC
if I call directly Restful service it's responding
if i make a test and put in home controller in web API HttpWebResponse it is responding
but the request is put in .js file and the code is
return $.ajax({
type: 'GET',
url: serviceURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: urlParams,
crossDomain: true,
beforeSend: lazy(),
complete: complete()
});
and this is failing with the following message e requested resource does not support http method 'OPTIONS
in webconfig everything is settled correctly
It is not working if I call REST API locally, if I call those which is on server it is working.
I read almost everything connected to that problem, but I am still stuck.
Here is my little first project as practice, having real trouble figuring out how to use JSON, took me a while to get it work locally but still no luck with servers, and tried few a including one i hosted, and even tried it with other hosted json files.
http://jsfiddle.net/Atlas_/Mgyc5/1/
$.ajax({
dataType: "json",
async: false,
url: "package.json", //https://www.dropbox.com/s/fmw63i4v7dtnx6t/package.json
'success': function (json) {
theQuiz = json.quiz;
console.log(json);
console.log(theQuiz);
}
});
When you access another domain be carefully with "Crossdomain".
To use with dropbox, try changing the URL to:
https://dl.dropboxusercontent.com/s/fmw63i4v7dtnx6t/package.json
Your code will be like this:
$.ajax({
dataType: "json",
async: false,
url: "https://dl.dropboxusercontent.com/s/fmw63i4v7dtnx6t/package.json",
success: function (json) {
theQuiz = json.quiz;
console.log(json);
console.log(theQuiz);
}
});
When you request 'dl.dropboxusercontent.com', you have this:
Access-Control-Allow-Origin: *
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control
"In this case, the server responds with a Access-Control-Allow-Origin:
* which means that the resource can be accessed by any domain in a cross-site manner."
Another options: Some websites (twitter, for example) work with "jsonp". http://en.wikipedia.org/wiki/JSONP or ..you can create your own proxy.
I am writing my first secure service that PhoneGap needs to access.
What data format is the most secure or appropriate to return?
I'm mostly interested in returning JSON, JSON-P, or OData but I can use XML if it's more secure.
Type of the call must be POST
The server expects JSON data (at least username and password).
The server sends JSON data back.
The code:
function makeCall(){
var URL = "HTTP://remote/server/rest/call";
var jsonData ='{"username":"'+$('#username').val()+'","password":"'+$('#password').val()+'"}';
$.ajax({
headers: {"Content-Type":"application/json; charset=UTF-8"},
type: "POST",
url: url,
data: jsonData,
dataType: "json",
success: succesFunction,
error: errorFunction
});
}
I have deployed my Spring project in Tomcat and startr the tomcat. I wrote one html page to call the spring MVC using ajax.
It says 405 Method Not Allowed.
I am wondering if i run the same url in browser it is working. Please any one can help me?
I have my html page in another folder
$.ajax({
type: "post",
url: "http://localhost:8080/SampleWebService/sample/sample-user",
cache: false,
data:'firstName=' + $("#firstName").val() + "&lastName=" + $("#lastName").val() + "¤tCompany=" + $("#currentCompany").val(),
success: function(response){
alert(response);
},
error: function(){
alert('Error while request..');
}
});
Could be a cross-domain problem. Set
crossDomain: 'TRUE'
The default value is FALSE for security reasons.
I have the following code, trying to get google's URL shortener to work.
$.ajax({
type: 'POST',
url: "https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDQ33gAu7thkpw_oW9VTcxR6YGhimcfik",
contentType: 'application/json',
data: '{ longUrl: "' + match +'"}',
dataType: 'jsonp',
success: function(id){
$('#menu').html(id);
}
});
The issue here is, when the datatype is just json, the request is made, but nothing is returned. when it is changed to jsonp, nothing happens at all. any ideas?
The JSON version doesn't work because it's a cross-origin call (see: Same Origin Policy). Does the Google URL shortener have a JSON-P API? It has to explicitly support it. (Also, JSON-P can't be POST; by its nature it's a GET.)
Update: Looks like they don't support it yet, but there's an open enhancement request.