I'm try to POST data with ajax by using DOJO(v. 1.8.1)
request.post("http://someweb.com/service", {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
handleAs: "text",
sync: false
}).then(function (text) {
console.log("The server returned: ", text);
});
When POST a data it doesn't show a log message in Tab Console of FireBug but shows in Tab Net instead.
NOTE : I'm already require "dojo/request" and log on dojo ready it's working OK. No any error.
Why it shows in Tab Net and how can I fix it? I have no idea what wrong with my code.
Related
I have a website in localhost:8080, that has a window to log in to Facebook. When I make the request like this:
function fb() {
$.ajax
(
{
type: "POST",
url: "https://www.facebook.com",
data: {
username: $("input[name = 'username']").val(),
password: $("input[name = 'password']").val()
},
success: function(data, text, status) {
alert(status.status);
}
});
}
it keeps returning me 200 status even if I provide incorrect details. I suppose it just returns me the whole page...
How can I make this work? I want the Facebook page opened and logged in if credentials are correct and if credentials are wrong, I want error code to be returned.
You cannot use ajax to post to another domain or the server has to enable CORS for your domain. But you can use the FB login: https://developers.facebook.com/docs/facebook-login/web/login-button
Both the WebAPI and the Website are sitting on localhost:80, so cross-domain shouldn't be an issue. Here's my client-side code:
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'http://localhost/SixFilmWebAPI/api/SixFilm/60',
dataType: "json",
success: function (data) { WriteResponse(data); },
error: function (err) { alert('error!'); },
});
});
function WriteResponse(actors) {
var strResult = "<table><th>Name</th><th>Dob</th><th>Dod</th>";
$.each(actors, function (index, actor) {
strResult += "<tr><td>" + message.Name + "</td><td> " + message.Dob + "</td></tr>" + message.Dod + "</td></tr>";
});
strResult += "</table>";
$("#divResult").html(strResult);
}
Each time I run my website, the error callback function gets hit, but, looking in Fiddler, I am getting my JSON object returned with a 200 OK response. Manually navigating to the WebAPI URL also returns JSON. What am I doing wrong???
Both the WebAPI and the Website are sitting on localhost:80, so cross-domain shouldn't be an issue.
In this case use relative url:
url: '/SixFilmWebAPI/api/SixFilm/60'
Then open your web browsers developer toolbar and inspect the Network tab. For example in Google Chrome's developer toolbar you will see the exact request being sent to the server and the response status code and body. This would give you a pretty good indication of what might have gone wrong.
Also place breakpoints inside your controller action and see if it gets hit. If it doesn't it's a routing or binding issue. In all cases the developer toolbar of your browser will help you sort it out. Also keep an eye on the Console tab which is where potential javascript errors and XHR warnings will be shown. Then look inside the success callback. Does it get hit? Did you return a JSON array from your Web API? Because from what I can see you are attempting to loop through the actors variable over there. So make sure that your server returned a JSON like this:
[
{ "Name":"foo", "Dob":"2010-11-01", "Dod":"2013-07-01" },
{ "Name":"bar", "Dob":"1967-05-17", "Dod":"2001-04-21" }
]
I make an async ajax call to the server, but the error callback is hit before getting response from the server (I can see it using fiddler):
$.ajax({
url: 'Login.aspx/AuthenticateRegularUser',
type: 'POST',
contentType: 'application/json; charset=utf-8',
async: true,
dataType: "json",
data: '{ "emailAddress": "' + emailAddress + '","password": "' + password + '","verificationCode": "' + verificationCode + '" }',
success: function(Result) {
if (Result != "") {
var ClientResponse = JSON.parse(Result.d);
if (ClientResponse.Success) {
//DO SUCCESS
}
else {
//DO FAIL
}
}
},
error: function(xhr, textStatus, errorThrown) {
//DO ERROR
}
});
It happens only when using IE11.
any ideas why?
I'm getting the same error.
For me, Fiddler shows that IE is not even sending the data to the server, and it crashes immediately.
My page does lots of ajax calls, but only one is crashing IE. The data part of the ajax call looks like this:
data: {
count: 0,
pid: 0,
vid: 0,
invalid: "86ddbe4a-841d-e111-a7fb-002269c41d11"
},
From the same page, and same code block, this one works fine (the vid is the only difference):
data: {
count: 0,
pid: 0,
vid: 32205,
invalid: "86ddbe4a-841d-e111-a7fb-002269c41d11"
},
This works perfectly fine in current Chrome and Firefox browsers, but crashes IE 11.
In tracing through the jquery-2.0.3 code, I can see that data being assigned correctly at line 7186, and at line 7845, hasContent is true and the data has a value, but the call to the server does not have any data in it (as seen via Fiddler).
Line 4362 is the last line in the debugger before the browser crashes with "Internet Explorer has stopped working".
I am not sure if it is the same but I am experiencing similar issue I reported here. Check if the conditions are the same (IE11 version, using https iframe inside an http parent window).
IE11 returns status 0 during Ajax POST operation from an iFrame (XMLHttpRequest: Network Error 0x2ee4)
I am writing a web-based application that makes a call to server to update a particular object in the server, from a REST-ful application.
This is the javascript code that I have written.
var sendUrl = url + "v1/items/" + itmId;
$.ajax({
"type": "PUT",
"url": sendUrl,
//"data": {"name": nameVal, "description": descVal},
"success": function() {
alert('hai'); //refreshItems();
},
"complete": function (jqXHR, textStatus) {
alert (textStatus);
}
});
When this code is executed, the server did not receive any request. The browser did not show any update.
I am using Google Chrome as my browser. So, I looked into Chrome's Javascript Console, the console showed this error:
Are there any possible solutions to this problem?
Edit:
As suggested in one of the comments, I tried looking into Network Panel. It seems it is not making a call to Server.
This is the screenshot of this particular request:
The image is very small. The status is "(failed)" and Type is Pending
If Server does not have PUT configured, it would result in an Internal Server Error. So, that is not happening here.
Edit
After trying the suggestion given in the answer,
This is the code:
var sendUrl = url + "v1/items/" + itmId;
$.ajax({
"type": "PUT",
"url": sendUrl,
//"data": {"name": nameVal, "description": descVal},
}).done(function ( ) {
alert('OK');
}).fail(function ( jqXHR, textStatus, errorThrown ) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
});
Here are the objects that has been logged:
Docs for jquery Ajax function
W3 specs regarding http/1.1 status codes
First, as stated in the jQuery documentation
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and
jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare
your code for their eventual removal, use jqXHR.done(), jqXHR.fail(),
and jqXHR.always() instead.
Also, the fail function accepts the following parameters:
jqXHR, textStatus, errorThrown
Since you're getting an error try the following code:
var sendUrl = url + "v1/items/" + itmId;
$.ajax({
"type": "PUT",
"url": sendUrl,
//"data": {"name": nameVal, "description": descVal},
}).done(function ( ) {
alert('OK');
}).fail(function ( jqXHR, textStatus, errorThrown ) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
});
This should output to the console usefull information about why the ajax call is not working.
If your webservice is working correctly, it should return a http status code. Here are some common response codes:
200 OK -> Everything is ok (it worked)
201 Created -> Ok too
400 Bad Request
401 Unauthorized
404 Not found
So, basically, the response you get should reflect why it's not working
I'm using jQuery 1.4.2 and am trying to perform a simple AJAX request. The target URL returns a JSON string (I validated it with jslint). The request works in Firefox and Chrome, but doesn't want to work in IE8, and I can't determine why. Here is the call:
jQuery.ajax({
url: 'http://' + domain + '/' + 'helper/echo/',
dataType: 'json',
success: function(data) {
alert(data);
},
beforeSend: function(request, settings) {
alert('Beginning ' + settings.dataType + ' request: ' + settings.url);
},
complete: function(request, status) {
alert('Request complete: ' + status);
},
error: function(request, status, error) {
alert(error);
}
});
IE will execute the beforeSend callback and the error callback. The error callback alerts with the message:
Error: This method cannot be called until the open method has been called.
My response header returns with Content-Type: text/javascript; charset=UTF-8.
What is going on with IE? I'm running the server on localhost, making a request from http://localhost:8080/psx to http://localhost:8080/helper. Maybe IE is blocking this request? I have tried installing Fiddler to analyze request traffic but it won't run on my machine because it's rather locked down. Firebug lets me, but everything seems good there.
Thanks for the help!!!
Alright, here's the fix! The request was using window.XMLHttpRequest(), which isn't working properly in IE8 for some reason. jQuery is not failing back to window.ActiveXObject("Microsoft.XMLHTTP") as it should.
Add this to your script somewhere before your AJAX call (only verified in IE8, not other IE's):
jQuery.ajaxSetup({
xhr: function() {
//return new window.XMLHttpRequest();
try{
if(window.ActiveXObject)
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch(e) { }
return new window.XMLHttpRequest();
}
});
Here's how I came to the solution:
Updated to jQuery 1.4.4 in case the issue was a bug that had been fixed.
Stepped through Firebug debugger and DevTools debugger until the results seemed to be drastically different.
On line 5899, the ajax() function creates the XmlHttpRequest object with the xhr() function. In Firefox, it was returning good data. In IE, this was returning with all fields being Error: This method cannot be called until the open method has been called.
I analyzed this function on line 5749, return new window.XMLHttpRequest();
I googled and came across this page that has the same problem and suggested the solution that works for me.
Official jQuery ticket: