jQuery.ajax() sends POST requests as GET in a Chrome extension - javascript

I'm building a small Chrome extension that must send messages through a POST http request to a server in my company network, and I'm using jQuery 1.4.1 to speed up the development of the javascript part.
I have this code to send the request:
function send() {
$.ajax({
url: "http://mycompany.com/update",
method: "POST",
data: {status: "sometest", in_reply_to_status_id: "anId"},
success: function(data, textStatus) {
console.log("success");
console.log(data);
console.log(textStatus);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("error");
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
},
complete: function(XMLHttpRequest, textStatus) {
console.log("complete");
}
});
}
The request done this way fails, in the Chrome log I see that the server responds with a http status 400 and with the text "This methods requires POST".
If I change to code above with this:
function send() {
$.post("http://sunshine.emerasoft.com/statusnet/api/statuses/update.xml", {status: "sometext", in_reply_to_status_id: "anId"}, function(data) {
console.log(data)
});
}
everything works fine, the http status is 200 and server side I can see that the data I sent is correctly saved.
I need to use the full $.ajax() method because I need to do some work in case of success or failure, and some other when the request is complete, so $.post() is not enough.
Am I doing something wrong calling $.ajax(), or there is an issue of some kind, maybe because I am in the xontext of a Chrome extension?
Thanks

I believe the $.ajax() function takes a 'type' option, not a 'method' option.
The default type is GET.

Related

Getting "parsererror" in jquery ajax

Below is my AJAX code. Here I'm hitting a service with one value. The service is getting called successfully, but it is not going into the success method. It is going into the error method only. In the error method it is giving parsererror and message: Unexpected token S
$.ajax({
type: 'GET',
url: 'http://domin.com:9000/ramsweb/rest/DetailRest/addOrderContacts/123456/' + customerId,
success: function (data, status, xhr) {
console.log(data);
$("#loadingSpinner").hide();
},
error: function (jqXhr, textStatus, errorMessage) {
$('.ErrorMsg').html('<h5>An error has occurred</h5>');
},
fail: function (data) {
$('.ErrorMsg').html('<h5>data loading failed</h5>');
}
});
jQuery AJAX functions by default will try to detect the type of response depending on other pieces of data in your request and response (headers etc.)
Most likely, your endpoint serves it as JSON thus telling jQuery to internally do a JSON.parse. However, your endpoint may be serving an error page instead of JSON which can cause parse errors like this.

response error from ajax http post crossdomain request with json

I am using asp.net and on client side in javascript and I try to make an ajax HTTP request:
$.ajax({
type: 'POST',
url: url,
crossDomain: true,
data: trip,
dataType: 'json',
success: function (responseData, textStatus) {
var value = responseData.someKey;
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
on the other side i have an azurecloud project and this method (c#):
[ActionName("test")]
[HttpPost]
public IHttpActionResult Test([FromBody] SetTripRequestEntity trip)
{
return Ok("year it works!");
}
If I run the code, the server jumps into the c# method and the trip has the correct object, so everything is fine.
But after he returns Ok("year it works!"); on the javascript side he runs into the error function:
responseData is an object with nulls and 0 parameters,
textStatus is "error" (really only "error" not more)
errorThrown is ""
I don't understand why he does not go to the success function.
I have tried to make the request in fiddler2 and there I got the right answer.
So the problem is at the javascript (ajax) side.
does somebody know how to run it?
Thanks a lot!

How to read response header of a ajax post call of empty response body

I am making a ajax post call that returns the required information in the response header an the empty response body . I am using the following code to make call
$.ajax({
url : someUrl,
type : "post",
contentType : "application/x-www-form-urlencoded",
success : function(data, textStatus, request)
{
alert("success");
},
error : function(request, textStatus, errorThrown)
{
alert("error");
},
timeout : "150000"
});
i always get error alert since the response is empty even though the status is 200 ok. Is there any workaround for the calls like this?
For example, we want to get Date header from the URL which returns error:
var get = $.get('http://stackoverflow.com/asdasdasddsadsadasde').always(function(){
alert(get.getResponseHeader('Date'));
});
success and error methods are deprecated in latest versions of jQuery. You may use done, fail and always methods.

Prevent IE10 from Redirecting to Login Page After 401 Error on AJAX Call

I have the following javascript code that performs a POST to an API for a login page:
do_post_form_data = function (relative_url, access_token, postData) {
return $.Deferred(function (def) {
$.ajax({
type: 'POST',
url: baseURL + relative_url,
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
data: postData
})
.done(function (data) {
def.resolve(data);
})
.fail(function (jqXHR, textStatus, errorThrown) {
var error = jqXHR.responseJSON ? jqXHR.responseJSON : { error: "", error_description: "POST failed." };
def.reject(error);
});
});
},
If the login fails (invalid credentials are sent), the API returns a 401 Unauthorized error to the caller with a JSON object in the body explaining why the login failed. The code works as expected when sending the data from the latest versions of Chrome, Firefox and Safari. The responseJSON object exists on the jqXHR object, and I can retrieve the error messages.
However, IE10 seems to be intercepting the 401 error and trying to redirect to a login page, for which the API server does not provide a URL, though I cannot see any additional calls being made to the server in Fiddler. My AJAX call never sees the 401 error but winds up receiving a 404 error with no JSON data in the jqXHR object.
Has anyone seen this behavior before and found a way to make IE report the initial 401 error to the AJAX call? IE10 with jQuery 2.0.2.
Thanks for any suggestions.

jQuery AJAX request on local development environment fail

I'm trying to test a jQuery/Javascript script on my local development environment. For some reason the script calls the error delegate. I've attempted to make sure I'm not performing a cross-domain request.
$.ajax({
url: 'http://localhost',
success: function(data, textStatus, jqXHR) {
console.log('success');
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
console.log(jqXHR);
}
});
return marker;
}
I'm running a WAMP stack. Here is my output:
error
undefined
XMLHttpRequest { mozResponseArrayBuffer=ArrayBuffer, status=0, more...}
Does anyone know what could be the issue?
Try replacing the absolute url:
url: 'http://localhost'
with a relative one:
url: '/'
If this doesn't solve your problem FireBug and/or Fiddler will give you more hints about what is going on and why the AJAX request fails.

Categories

Resources