The fail function gives me a correct output, whereas the done function gives me blank. I am stuck with this for hours. Any idea on how to debug this?
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
if (contentType == null) {
contentType = "application/json";
}
var reqst = $.ajax({
beforeSend : function(req) {
req.setRequestHeader("Content-Type", "application/json");
},
url : url,
type : "post",
contentType : contentType,
data : paramsTable,
});
reqst.done(function(xhr){
alert(xhr.responseText);
});
reqst.fail(function(xhr){
alert(xhr.responseText);
});
I would like to recommend you to use Ajax jQuery API instead of the old Ajax requests. No body uses it today (almost nobody; since you use it)
$.ajax({
beforeSend: function(req) {
req.setRequestHeader("Content-Type", "application/json");
},
url: url,
type: "post",
contentType: contentType,
data: paramsTable,
success: function () {
// here the code to execute if success
},
error: function () {
// here the code to execute if error..
}
});
Why your code isn't working
Because you are never using the xhr to send the Ajax request so it has nothing to show as the response text. Funny isn't it? hehehe.
use this:
success: function(resp) {
alert(resp); // where resp is the name of the responseText here..
}
I have shown you how to use it. Good luck!
For more: http://api.jquery.com/jquery.ajax/
Try to use standard jquery ajax structure:
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$( this ).addClass( "done" );
})
.fail(function() {
alert( "error" );
});
Related
I am trying to send my selection to a server using post and server need REST, Tested my data with Advanced Rest Client and all works well, But when I try from my chrome extension I am getting undefinded error. Here is what I am trying
chrome.runtime.sendMessage({
method: 'POST',
action: 'REST',
headers: {'Remote-User':'myuser'},
url: 'http://myserver/data/add/',
data: {'content': 'the paste content'}
}, function(responseText) {
alert(responseText);
});
Any help much appreciated, thanks
Update
Based sdc, I changed my code like this and still responseText is undefined :(
function genericOnClick(info, tab)
{
var url = 'http://mysite/data/add/';
var data = $.toJSON({'content': 'the paste content'});
$.ajax({
headers: {
'Remote-User':'username',
'Content-Type':'application/json;charset=utf-8'
},
url: url,
data: data,
method: 'POST',
dataType: 'json',
error: function(xhr, status, error){
alert(xhr.responseText);
},
success: function(data){
console.log('succes: '+data);
}
});
}
I think you misunderstand the purpose of chrome.runtime.sendMessage. See the first two paragraphs of the Message Passing documentation. sendMessage is used for "communication between extensions and their content scripts" it is not designed for sending an HTTP request.
A content script must also "set up a runtime.onMessage event listener to handle the message" only then will you receive a valid response from the sendMessage request
The result from their example is undefined
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
<- undefined
If you are attempting to perform a HTTP request from within your chrome extension, you should use XHR
var data = $.toJSON({'content': 'the paste content'});
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://myserver/data/add/", true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Content-length", data.length);
xhr.setRequestHeader("Connection", "close");
xhr.setRequestHeader('Remote-User', 'myuser');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
console.log('xhr response: '+ xhr.responseText);
}
}
xhr.send(data);
I'm using a Jquery Ajax POS request to send JSON data from my chrome extension to my server, then having a response sent containing javascript to be executed on the client side.
How should I set my contentType and dataType to achieve this? I'm currently getting a 'contentType is not defined' error message.. code below:
client.js
request("https://a304cff8.ngrok.io/api/panels/hitme", "post", "json", {apples})
.done(function(res){
console.log(res)
})
})
function request(url, method, contentType, data){
return $.ajax({
url: url,
method: method,
contentType: contentType,
data: data
})
}
function demo () {
$(".abc").click( function(e){
if(e.which == '10' || e.which=='13') {
e.preventDefault();
return;
}
var xyz=$(this).val();
$.ajax({
url:'jquery-data.php',
type:'GET',
dataType:'JSON',
data:{........},
success:function(data,textStatus,jqXHR) {
// showing return status
}
});
});
}
I try to use an ajax request to save a category option and wonder how could I detect whether my ajax request has been successfully handled by ajax_attributes.phtml. My issue is no matter what url I specify, it always alerts it works, even I change the url into something like yadayada.phtml,which is not existed at all.
new Ajax.Request('ajax_attributes.phtml',
{
parameters: {categoryId: categoryId},
onSuccess: function(response) {
// Handle the response content...
alert('it works');
},
onFailure: function(){
alert('Something went wrong...');
}
});
You can try with this code :
var request = $.ajax({
url: "script.php",
type: "POST",
data: { categoryId: categoryId},
dataType: "html"
});
request.done(function( msg ) {
alert('it work');
});
request.fail(function( jqXHR, textStatus ) {
alert('Something went wrong...');
});
I want to send JSON data in XDR using the POST method. I'm able to send JSON data, however the problem is . (DOT) symbols are converted into _ (underscores). Here is the code:
if ($.browser.msie && window.XDomainRequest) {
var xdr = new XDomainRequest();
xdr.open("POST",Path);
xdr.send(JSON.stringify(data) + '&ie=1');
xdr.onerror = function() {
alert('in error');
};
xdr.onload = function() {
alert(xdr.responseText);
}
} else {
jQuery.ajax({
type: "POST",
url: Path,
data: JSON.stringify(data),
dataType: 'json',
contentType: 'application/json',
success: function(msg) {
alert(msg);
}
});
}
May be your server side scripting is wrong..
this code seems to be correct....
Hello I have read about jQuery ajax method, jQuery wraps some XMLHttpRequest inside.
I need(want) to redo next stuff to $ajax, but I worried about parameters. Is it possible send to $ajax url with parameters without param key, just in URL param ?
var element ;
...
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
http_request.onreadystatechange = function() { alertContents(element); };
url = unescape(url + parameters);
http_request.open('GET', url, true);
http_request.send(null);
Use this same code for ajax consumes the time
$.ajax({
type: "get",
url: "geocode.php?address="+address,
dataType: "xml",
//async:true,
success : function(xmlData){
alert(xmlData);
},
error : function(){
alert("Could not retrieve XML file.");
}
});
if you want to use $.ajax you could write
$.ajax({
url: url,
type: "GET",
data: parameters
success: function(data) {
alertContents(element);
}
});
Pretty much anything you can do using the XMLHttpRequest you can do with jQuery (jQuery uses XMLHttpRequest for ajax).
$.ajax({
url: url + parameters,
type: "GET",
success: function(data){
alert(data);
}
});
you could also set the parameters in the data option so that the type option will send it as POST or GET based on what you supply to it.
$.ajax({
url: url,
data: parameters,
type: "GET", // or POST, or any other valid type
success: function(data){
alert(data);
}
});
If I'm understanding your question correctly, the answer is yes, you can.
The following are equivalent:
$.ajax({
url: "some.php?name=John&location=Boston"
});
$.ajax({
url: "some.php",
data: "name=John&location=Boston"
});
$.ajax({
url: "some.php",
data: {
name : "John",
location : "Boston"
}
});