I am using following code to call java web API
m$.ajaxq({
url: contextPath + "/updateElapsedTime",
type: "POST",
data: params,
contentType: "application/json; charset=utf-8",
dataType: 'text',
async: optionalRunAsync,
success: function (response) {
console.log("Success")
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("Exception name is ...");
}
});
Now there is a case when it throws custom exception.
I want to know which custom exception is thrown(classname) from java api and print it in javascript.
Check if response is not 200.
then use...
xhr.responseText
this will gives your custom error message
Related
I'm trying to make ajax post request to my flask app and get the answer in the callback function. Unfortunately I got undefined value instead of a json.
Will appreciate any help!
My server-side code:
application = Flask(__name__)
CORS(application)
...
#application.route('/get_forecast', methods=['POST', 'GET'])
def get_forecast():
if request.method == 'POST':
print('in post')
predictions = {}
predictions['data'] = calc_forecast(request.get_json()["data"])
print(predictions)
return jsonify(predictions)
my client-side code:
$.ajax({
type: "POST",
url: "http://localhost:5000/get_forecast",
data: JSON.stringify(markers),
dataType: 'json',
crossDomain: true,
contentType: "application/json",
done: function(resp, status){
console.log('done')
console.log(resp)
console.log(status)
console.log('=====')
}(),
fail: function(xhr, ajaxOptions, thrownError){
console.log('fail');
console.log(thrownError)
}()
});
Here's what I got in the flask terminal:
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [02/Dec/2018 12:59:53] "OPTIONS /get_forecast HTTP/1.1" 200 -
in post
{'data': [10.15451836569513, 56.578480707072714, 12.435548873275337]}
127.0.0.1 - - [02/Dec/2018 12:59:53] "POST /get_forecast HTTP/1.1" 200 -
Here's what I got in the chrome console:
demo.html:228 done
demo.html:229 undefined
demo.html:230 undefined
demo.html:231 =====
demo.html:234 fail
demo.html:235 undefined
If you look at the examples over at http://api.jquery.com/jQuery.ajax/ they look quite a bit different from your call to $.ajax. I adjusted your code to better resemble the examples. Maybe this helps?
$.ajax({
type: "POST",
url: "http://localhost:5000/get_forecast",
data: JSON.stringify(markers),
dataType: 'json',
crossDomain: true,
contentType: "application/json"
})
.done(function (data, textStatus, jqXHR) {
console.log('done');
console.log(data);
console.log(textStatus);
console.log('=====');
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log('fail');
console.log(textStatus);
});
I think, you were looking for success and error settings.
Update: Yes, I'm pretty sure, your settings done and fail are wrong. You want to use success and error.
Also, have a look at https://stackoverflow.com/a/10931891/1621041
This means, your code could also look like this, if you want to put everything into the settings of the $.ajax call:
$.ajax({
type: "POST",
url: "http://localhost:5000/get_forecast",
data: JSON.stringify(markers),
dataType: 'json',
crossDomain: true,
contentType: "application/json",
success: function (data, textStatus, jqXHR) {
console.log('done');
console.log(data);
console.log(textStatus);
console.log('=====');
},
error: function (jqXHR, textStatus, errorThrown ) {
console.log('fail');
console.log(errorThrown);
}
});
I'm trying to capture data from an HTML page that is on the another website. I need to capture that data and save it into my site. That's why I used cross domain ajax like this
var myCallback = function(data) {
console.log(data);
};
var formData = $('.data-capture-form').serialize();
$.ajax({
url: 'http://prospectbank.co.uk/leads/capt',
type: 'GET',
data: formData,
dataType: 'jsonp',
crossDomain: true,
jsonp: 'callback',
jsonpCallback: 'myCallback'
}).done(function(res) {
console.log(res);
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
});
Then I get this error
Any help? Thanks
The response from http://prospectbank.co.uk/leads/capt?_=1340513330866? is wrong formatted JSON.
{"status":true}
It should be
{status:true}
i got from my customer string that I need to get to the server and get an answer.
this is the string:
http://XXX.YYY.DDD.WWW:8082/My_ws?Myapp=Myprice&Mydip=123&itemno=123456
and i get any string as Result.
i try this in ajax and jQuery:
$.ajax({
ServiceCallID: 1,
url: MyString,
type: 'POST',
data: '{"itemno": "' + itemno+ '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success:
function (data, textStatus, XMLHttpRequest) {
ALL = (data.d).toString();
}
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
how to "talk" with the server and get the Result from my string ?
thanks
I'm trying to get some information from a different domain, the domain allows only jsonp call - others get rejected. How can I get the content instead of execution? Because I get an error in response. I don't need to execute it, I just need it in my script. In any format (the response is json but js doesn't understand it).
I can't affect on that domain so it's impossible to change something on that side.
Here's my code:
$.ajax({
url: url + '?callback=?',
crossDomain: true,
type: "POST",
data: {key: key},
contentType: "application/json; charset=utf-8;",
async: false,
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
error: function(xhr, status, error) {
console.log(status + '; ' + error);
}
});
window.jsonpCallback = function(response) {
console.log('callback success');
};
There are a few issues with your $.ajax call.
$.ajax({
url: url + '?callback=?',
// this is not needed for JSONP. What this does, is force a local
// AJAX call to accessed as if it were cross domain
crossDomain: true,
// JSONP can only be GET
type: "POST",
data: {key: key},
// contentType is for the request body, it is incorrect here
contentType: "application/json; charset=utf-8;",
// This does not work with JSONP, nor should you be using it anyway.
// It will lock up the browser
async: false,
dataType: 'jsonp',
// This changes the parameter that jQuery will add to the URL
jsonp: 'callback',
// This overrides the callback value that jQuery will add to the URL
// useful to help with caching
// or if the URL has a hard-coded callback (you need to set jsonp: false)
jsonpCallback: 'jsonpCallback',
error: function(xhr, status, error) {
console.log(status + '; ' + error);
}
});
You should be calling your url like this:
$.ajax({
url: url,
data: {key: key},
dataType: 'jsonp',
success: function(response) {
console.log('callback success');
},
error: function(xhr, status, error) {
console.log(status + '; ' + error);
}
});
JSONP is not JSON. JSONP is actually just adding a script tag to your <head>. The response needs to be a JavaScript file containing a function call with the JSON data as a parameter.
JSONP is something the server needs to support. If the server doesn't respond correctly, you can't use JSONP.
Please read the docs: http://api.jquery.com/jquery.ajax/
var url = "https://status.github.com/api/status.json?callback=apiStatus";
$.ajax({
url: url,
dataType: 'jsonp',
jsonpCallback: 'apiStatus',
success: function (response) {
console.log('callback success: ', response);
},
error: function (xhr, status, error) {
console.log(status + '; ' + error);
}
});
Try this code.
Also try calling this url directly in ur browser and see what it exactly returns, by this way You can understand better what actually happens :).
The jsonpCallback parameter is used for specifying the name of the function in the JSONP response, not the name of the function in your code. You can likely remove this; jQuery will handle this automatically on your behalf.
Instead, you're looking for the success parameter (to retrieve the response data). For example:
$.ajax({
url: url,
crossDomain: true,
type: "POST",
data: {key: key},
contentType: "application/json; charset=utf-8;",
async: false,
dataType: 'jsonp',
success: function(data){
console.log('callback success');
console.log(data);
}
error: function(xhr, status, error) {
console.log(status + '; ' + error);
}
});
You can also likely remove the other JSONP-releated parameters, which were set to jQuery defaults. See jQuery.ajax for more information.
I'm trying to get JSON data from another domain.
It returns status 200 but invokes an error in the callback function.
Is it possible to get the raw data or html or even just text when I get into the error callback?
Why is xhr.responseText undefined?
Here is part of my code:
$.ajax({
type: 'POST',
url: 'http://timetable.nctu.edu.tw/?r=main/get_cos_list',
dataType: 'jsonp',
crossDomain: true,
async: false,
data:{m_acy:'**',m_sem:'**',m_degree:'**',m_dep_id:'**',m_group:'**',
m_grade:'**',m_class:'**',m_option:'cos_code',m_crsname:'**',m_teaname:'**',
m_cos_id:'**',m_cos_code:'DAM1346',m_crstime:'**'},
jsonp: true,
success: function(json) {
$('#jjjj').html(json);
},
error: function(xhr, textStatus, errorThrown) {
$('#content').append("readyState: "+xhr.readyState+"<br>status: "+xhr.status+"<br>responseText: "+xhr.responseText+"<br><hr>");
alert('Ajax request error.' + xhr.responseText + xhr.responseData + errorThrown);
},
complete: function(xhr, textStatus)
{
alert(xhr.responseText + xhr.responseHtml);
alert(h.responseHtml);
}
});