I have an apache cordova project where I'm trying to post form data from the client to the database. I can see the json I send going out to my API which then sends the data to the DAL and then to the database. The entry correctly updates the table and then returns a 1 or -1 based on whether or not a previous entry exists. This value is then sent to the client. I can see the exact response being sent back, but my ajax success function won't fire.
Ajax:
$("#submit").click(function () {
var info = {
Username: $("#username").val(),
Password: $("#password").val(),
Firstname: $("#firstname").val(),
Lastname: $("#lastname").val(),
Email: $("#email").val()
};
info = JSON.stringify(info);
event.preventDefault();
$.ajax({
type: "POST",
dataType: "json",
async: false,
url: "http://localhost:57207/api/User/RegisterUser",
data: info,
success: function(data) {
console.log(data);
},
error: function(input) {
console.log(JSON.stringify(input));
}
})
});
Controller:
[System.Web.Http.HttpPost]
public JObject RegisterUser(JObject obj)
{
RegisterUsers user = new RegisterUsers();
user = JsonConvert.DeserializeObject<RegisterUsers>(((JProperty)obj.First).Name);
var temp = DataAccessLayer.RegisterUser(user.Username, user.Password, user.Firstname, user.Lastname, user.Email, null, null);
JObject jobj = new JObject();
jobj.Add("output", temp.ToString());
return jobj;
}
I have been using fiddler to check my response and request, and here is the raw data:
Request:
POST http://localhost:57207/api/User/RegisterUser HTTP/1.1
Host: localhost:57207
Connection: keep-alive
Content-Length: 125
Accept: text/html, */*; q=0.01
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:4400/test.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
{"Username":"johndoe","Password":"password!!!","Firstname":"john","Lastname":"doe","Email":"johndoe#random.com"}
Response:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcRGV2ZWxvcG1lbnRcVGV4dDJQaG9uaWNzXFJlc3RBUElcYXBpXFVzZXJcUmVnaXN0ZXJVc2Vy?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,POST,OPTIONS
Access-Control-Allow-Headers: Content-Type, soapaction
Date: Wed, 08 Feb 2017 19:52:43 GMT
Content-Length: 15
{"output":"-1"}
My error function is the one that fires, and here is the output:
console.log:
{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:57207/api/User/RegisterUser'."}
Update:
I was able to set the async to true and my new error message is this:
{"readyState":0,"status":0,"statusText":"error"}
Try instead returning an IHttpActionResult or IActionResult, Ok() for a 200 response.
public IActionResult RegisterUser(JObject obj)
{
RegisterUsers user = new RegisterUsers();
user = JsonConvert.DeserializeObject<RegisterUsers>(((JProperty)obj.First).Name);
var temp = DataAccessLayer.RegisterUser(user.Username, user.Password, user.Firstname, user.Lastname, user.Email, null, null);
JObject jobj = new JObject();
jobj.Add("output", temp.ToString());
return Ok(jobj);
}
If it continues to fail, try just return a simple string like return Ok("Hello World!");. If it succeeds with simple string, then the serialization of the object may be failing or is coming out as invalid JSON.
Related
I have the following server method for returning an byte[] for an xls document stored in Azure Blob Storage.
[FunctionName("ReadBatchFile")]
public async static Task<HttpResponseMessage> ReadBatchFile([HttpTrigger(AuthorizationLevel.Function, WebRequestMethods.Http.Get, Route = "Agreements/ReadBatchFile")]HttpRequestMessage req, TraceWriter log)
{
try
{
var fileName = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "fileName", true) == 0)
.Value;
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(await AzureHelpers.ReadFromBlobStorage(fileName)); //returns byte[]
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = fileName };
response.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
return response;
}
catch (Exception ex)
{
log.Error(ex.Message);
req.CreateResponse(HttpStatusCode.InternalServerError);
}
return req.CreateResponse(HttpStatusCode.BadRequest);
}
As can be seen from the above, the response.Content is set an ByteArrayContent and my contentType is application/octet-stream.
In Angular, I have the following method in my service to call the above function.
readBatchFile(fileName: string): Observable<Blob> {
let endpoint = environment.endpoints.agreement.baseUrl + environment.endpoints.agreement.readBatchFile + "&fileName=" + fileName;
const httpOptions = {
headers: new HttpHeaders({ contentType: 'application/octet-stream' })}; //application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
return this.http.get<Blob>(endpoint, httpOptions);
}
Although I am specifying that the contentType is application/octet-stream, and that my get method should return a Blob type, I still get the following error:
Unexpected token P in JSON at position 0↵ at JSON.parse
()
Why is Angular still seeing the response from the server as JSON?
Update
Here is my request headers:
Accept: application/json, text/plain, */*
Authorization: Bearer ...
Origin: http://localhost:4200
Referer: http://localhost:4200/batches
responseType: application/octet-stream
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
Response Headers:
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Content-Disposition: attachment; filename=B4debe034-efc6-416f-8082-422a4122a857_MTN_Upgrade_20181017_T065548.xls
Content-Length: 3011
Content-Type: application/octet-stream
Date: Wed, 17 Oct 2018 06:55:48 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
And the Response:
PKø6QMäH¯3[Content_Types].xmlµÏJ1Æ_eÉU´D¤ÛªU°>ÀÌvCóÌ´¶oo6+"¥z$ßÌ÷ý3_¼köÉÆÐÆM+ÞÖ[ÑC0àbÀVÄr1_RSfµ¢gNwJîÑÉ0¥ÙkÞ¨zT×ÓéÒ10ðà!ó{ì`ç¸Yïu+ %g5pÁRÅL4"Ã]ýanÌ ÌäDftµzèê4 ¨4$<ÉÖà¿"b×Y&ê/#RF0Ô#²w²VéÁ1ô2?/®êàÔGÌÛ÷·²j"êù·ü*ªevAâ£C:G1*î!£yå\ü<ÁÏoU~ñ PKäH¯3PKø6QMÚë®'_rels/.relsÏÁ0àWYzc1ájðæVÖe
oïb<xlú÷ûÓ²^æ=Ѭ"Ë¡U¤kÜÚËî,DiµÈ¢ÔUyÅIÆtúÁô1ºçAõ8ËC6ùYÆ4zÃT£4È÷y~àþÓÉ-À7ºÖ®ÿ±©ëgRmüQñH²ô£eâ/òãhÌ
¼*ùæÁê
PKÚë®'PKø6QMæl($×Sxl/workbook.xmlO½NÄ0~È;ÂpBUÛ`9 é{Hkt]9)åÝx$^ôª
F&ö÷cûûó«9|Ä ÞQgjávWB²Ü{:·0ews®Y.oÌUÔjiaÈy¬µNvÀhÒG¤Â9hriå¬Ù9oñí²¾«ª½&Miðc5í?Yi4}skT4 k«^=Îé÷È¥Uºkôîjݪ"±$Muûò>(©}rì^26£5Á>rSèÍêXTÛêîPKæl($×SPKø6QMb¢Ö4xl/_rels/workbook.xml.relsÏjÃ0_Åè¾8é`Q·1èµ#ØJØÆÒÚåík6VR(cdôý>¬åúkÔ2÷1hª]:ýÇÓ+(ÈÀDëÕrKJYaß'VØIoZ³õ4"W1Q(/mÌ#Jis§Ú#v¤uý¢ó·LµqòÆ5 öS¢ÿ°cÛöÞ£ý)È}ùÈH
sGbà:bý]ªPAßY<ReÊ_^M~ú¿âï1ÛI.[ÌÇ¿2úæÚ«PKb¢Ö4PKø6QMÄΫhïxl/worksheets/sheet1.xmlËr0F_a_Ðýâ±Iã:ñ®ÓE»¦¶äpwë¢ÔW¨ÀÄ7 Ðù~I
ðïÏßùÃG~Þ±.!#¨b«wYñºOåþó³6o6Uª^ØYiYgql·©Êé£*\ß^<)Ý¥yõ~mÕJoO¹*ÊÀb£Ié²iv´á¥Ú=µìѨdWO!?\JåIVËù.sÕ«ÙFíá#mHÄËy
ÿÌÔÙÞ´j%¿µ~«.6»Ej6öàu=øwìÔ>9Êúü¢²×´thÙêAUê O>êó9Û©k¡!Â\ÇödKÿjºÃø&8jã<J±LFã¸ãÛÑ!Å $ÆI'·q! ¥
Î)<mâôÇe!8ÀÑÑ8kâìÀ
/³Ñ8oâüg#&¥ «M\\ã p½Clâò§nòHp¡tÒñeóÔ;mÉrnô90U¯+]5q[oO·ã¬»û¾óø½6ÄW]âÉ'PXùîß|tµOÐ.ñì¬K¼øï-;{BÔ*D^DöúìY~#zWH-c "$§C"z6§«ÞS{#zíezeQ¤£·Jñ¤Rð }¥ø~¥ÓÕÖH_éÒW:½²Í(ÒQJZ¥dR©OxJ¾Rr¿Òéj뤯té+^Ùf¹(oÞ£©ûMPfu©Lýeo]ÿPKÄΫhïPKø6QM"tÛ4SÎ
xl/styles.xmlR½nà ~ÄÞGjUU¶3T²Ô%K:tÅ6ØHp ÀÝWëÐGê+°8Q¥Lw|þ~ð?_ßù~TuBC³Í#nt<xÆû2w~ìØ3æQà+pï½y!Ä5=SÔm´a¾pmõáh;âe´uQ¤$Ùm·ODQ¸ÌaPò5z_àIÊk¸#üNT$K, ÍÀ+¢¶"¡dæ¦âRHy¶Úá(sC½gªp#Kÿ>V`ÐÀD¼Cï,²ÝãZJH®µmÃ,׿1Ce.÷QaEקÆkK½×*v *ïliwä<Æ=|ð«£y omeÂÒF£µl6¹£§ÆÈé0¨Ù*3ÒÖ®PéYF~vÎI©WQgÅ=øýåÊ£ôþ»~0mG~óhÈåa¿PK"tÛ4SÎPKø6QMPùûxl/sharedStrings.xmlm]k0ÿJÈ}h][Z2è2Ðþ ÇfsÛ¿_ÆØÖË<ÏÄ=±wp8Zð`/9ÓØv4}ÂoõóîÌ2¬rvIcDbþªÁDó£Ø îíÆÎ:È]/pv ZHO"ò(´
g]ù²gßÈþ/1¦1¥Euò2ÆâüÐêZ°Ò®ijÇ2ÛÂRtÇ^TóªzØÌÿºZ¹±ë6#O
ù¥1¾o$h×¾¨Kû¥mñÌj
píä)ÑÃñt^WdT6p åNkq{§þ!ü¦_PKPùûPK-ø6QMäH¯3[Content_Types].xmlPK-ø6QMÚë®'Y_rels/.relsPK-ø6QMæl($×S#xl/workbook.xmlPK-ø6QMb¢Ö4Txl/_rels/workbook.xml.relsPK-ø6QMÄΫhïrxl/worksheets/sheet1.xmlPK-ø6QM"tÛ4SÎ
xl/styles.xmlPK-ø6QMPùû®xl/sharedStrings.xmlPKÂë
For Angular 8+
return this.http.get(endpoint, { responseType: 'blob'});
return this.http.post(endpoint, postParams, { responseType: 'blob'});
It is subtle but if you look at the docs you will notice http.post and http.get does not take a generic return type of <T> when we set responseType: 'blob'. So we don't need the <Blob> part in http.get<Blob> anymore. Which makes sense when you think about it since we are telling HttpClient the response is of type blob with responseType: 'blob'.
https://angular.io/api/common/http/HttpClient#post
If you need something besides 'blob' here are the other options available for responseType.
responseType: 'arraybuffer' | 'blob' | 'json' | 'text'
https://angular.io/api/common/http/HttpRequest#responseType
Starting from Angular 9 there are four response types supported: 'arraybuffer'|'blob'|'json'|'text'.
For file contents you want to use the blob response type, which you can set in the get-request directly like this:
return this.http.get(endpoint, { responseType: 'blob'});
More information is here in the docs.
Your content-type header is only used for the request-header, it does not influence the response.
Old Answer (up to Angular 8)
You can set the responseType in the get-request directly like this:
return this.http.get(endpoint, { responseType: 'application/octet-stream'});
I resoloved the problem in Angular 9.1.4 by using responseType blob and setting type in BlobPropertyBag later, e.g. for Excel files:
return this.httpClient.get(url, {
responseType: 'blob',
params: queryParams
})
.pipe(
map((res: any) => {
const blob = new Blob([res], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
return blob;
})
);
I resolve this On angular 9.1.0 with following code:
result = this._httpClient.get(url,
{
responseType: 'blob',
headers: headers
});
Wish it help anyone..
{ responseType: 'blob' as 'json'}
doesn't go in the headers object..
Is it possible to retrieve a list of entities as a parameter for an action in c# odata?
Tried the following but it casues the entire controller to not work:
[HttpPost]
[NhSessionManagement()]
[ODataRoute("BatchUpdate")]
public async Task<IHttpActionResult> BatchUpdate(List<Item> items, bool updateDefaultJSONFile)
{
return Ok();
}
This is the config:
{
var action = builder.EntityType<Item>().Collection.Action("BatchUpdate");
action.CollectionParameter<Item>("items");
action.Parameter<bool>("updateDefaultJSONFile");
}
This is how I then send the data:
$http.post(appConfig.serviceUrl + "api/items/Service.BatchUpdate", { items: itemsToUpdate, updateDefaultJSONFile: true}).success(function (data, status, headers, config) {
debug("Successfully saved");
}).error(function (data, status, headers, config) {
debug("Failed to save");
});
In fiddler:
POST http://192.168.20.108/api/items/Service.BatchUpdate HTTP/1.1
Host: 192.168.20.108
Connection: keep-alive
Content-Length: 83114
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, */*
Origin: http://192.168.20.108
Authorization: Bearer ****
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://192.168.20.108/
Accept-Encoding: gzip, deflate
Accept-Language: sv-SE,sv;q=0.9,en-US;q=0.8,en;q=0.7
{"items":[{"Key":"helpful","Text":"test"}],"updateDefaultJSONFile":false}
Specify collection parameters as an array, not a list:
[HttpPost]
[NhSessionManagement()]
[ODataRoute("BatchUpdate")]
public async Task<IHttpActionResult> BatchUpdate(Item[] items, bool updateDefaultJSONFile)
{
return Ok();
}
Config is correct, good job.
I'm new to WebRequests and I tried this:
string json = #"{""Gebruikersnaam"":""user"",""Wachtwoord"":""pass"",""IngelogdBlijven"":true}";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://pontessg.magister.net/api/sessies");
httpWebRequest.ContentType = "application/json";
httpWebRequest.ContentLength = json.Length;
Console.WriteLine(json.Length);
Console.WriteLine(json);
Console.Read();
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
My code is keep returning me ERROR (400) and I don't really know why I think I forgot something, but I don't know what
(The Headers)
Request URL:https://pontessg.magister.net/api/sessies
Request Method:POST
Status Code:200 OK
Remote Address:[...]
Response Headers
view parsed
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 76
Content-Type: application/json; charset=utf-8
Expires: -1
Date: Sat, 25 Feb 2017 21:45:33 GMT
X-Frame-Options: DENY
Strict-Transport-Security: max-age=[...]
Request Headers
view parsed
POST /api/sessies HTTP/1.1
Host: pontessg.magister.net
Connection: keep-alive
Content-Length: 88
Accept: application/json, text/plain, */*
X-API-Client-ID: 12D8
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Origin: https://pontessg.magister.net
Content-Type: application/json;charset=UTF-8
DNT: 1
Referer: https://pontessg.magister.net/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8
Cookie: SESSION_ID=[...]; M6UserName=user; username=pass; Magister.UserName=user; [...]
Request Payload
{"Gebruikersnaam":"user","Wachtwoord":"pass","IngelogdBlijven":true}
When I try the JS code below in the console of my browser, it returns 200 OK
$.ajax({
url: "https://pontessg.magister.net/api/sessies",
type: "POST",
data: "{\"Gebruikersnaam\":\"user\",\"Wachtwoord\":\"pass\",\"IngelogdBlijven\":true}",
contentType: "application/json;charset=utf-8",
success: action_Succeeded,
error: action_Failed
});
function action_Succeeded(r) {
console.log("succes");
}
function action_Failed(r1, r2, r3) {
alert("fail");
}
If someone could help me with this, I would like that
I have a Flight PHP REST server set up.
At 1 end point it expects POST data and based on one of the POST data it retrieves some data from the database and returns it as JSON.
When I use the Postman REST extension in Chrome I see the correct result. But when I do the call using my C# application the returned json is null because the $_POST seems empty.
This is my Flight index.php:
Flight::route('POST /getText', function(){
// Create a new report class:
$reportText = new ReportText;
$theText = $reportText->ParsePost($_POST);
if ($theText == null)
{
echo null;
}
else
{
echo json_encode($theText);
}
});
This is my ParsePost:
public function ParsePost($PostDictionary)
{
$textArray = null;
foreach ($PostDictionary as $key => $value)
{
if (!empty($value))
{
list($tmp, $id) = explode("_", $key);
$text = $this->geFooWithText($id);
$textArray[$key] = "bar";
}
}
return $textArray;
}
This is my C# part:
private static async Task RunAsyncPost(string requestUri)
{
using (var client = new HttpClient())
{
// Send HTTP requests
client.BaseAddress = new Uri("myUrl");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
// HTTP POST
var response = await client.PostAsJsonAsync(requestUri, new { question_8 = "foo", question_9 = "bar" });
response.EnsureSuccessStatusCode(); // Throw if not a success code.
if (response.IsSuccessStatusCode)
{
var json = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(json))
{
throw new ArgumentNullException("json", #"Response from the server is null");
}
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
foreach (var kvp in dictionary)
{
Debug.WriteLine("Key: {0}, Value: {1}", kvp.Key, kvp.Value);
}
}
}
catch (HttpRequestException e)
{
// Handle exception.
Debug.WriteLine(e.ToString());
throw;
}
}
}
This is the response from Postman:
{
"question_8": "foo",
"question_9": "bar"
}
It seems I'm missing something in my call using C#.
[Update]
In this post (Unable to do a HTTP POST to a REST service passing json through C#) the same problem seems to appear.
Using Fiddler was suggested.
Using Postman and x-www-form-urlencoded:
POST http://myHost/api/getText HTTP/1.1
Host: myHost
Connection: keep-alive
Content-Length: 29
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
question_8=foo&question_9=bar
Using Postman and form-data:
POST http://myHost/api/getText HTTP/1.1
Host: myHost
Connection: keep-alive
Content-Length: 244
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvb4wmaP4KooT6TFu
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
------WebKitFormBoundaryvb4wmaP4KooT6TFu
Content-Disposition: form-data; name="question_8"
foo
------WebKitFormBoundaryvb4wmaP4KooT6TFu
Content-Disposition: form-data; name="question_9"
bar
------WebKitFormBoundaryvb4wmaP4KooT6TFu--
Using my C#-application:
POST http://myHost/api/getText/ HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Host: myHost
Content-Length: 50
Expect: 100-continue
Connection: Keep-Alive
{"question_8":"foo","question_9":"bar"}
The C# application is sending it clearly in a different way.
I've found the problem.
Because I use client.PostAsJsonAsync() the POST data is send as json, as you can see in Fiddler.
PHP doesn't expects the POST data to be in json format.
To read that data I need to use $data = file_get_contents('php://input'); in my PHP file.
Now I have my keys and values and can I continue. It looks like PHP needs a $_JSON variable ;)
I have been sorting through this issue all day and hope someone can help pinpoint my problem. I have created a "asynchronous progress callback" type functionality in my app using ajax. When I strip the functionality out into a test application I get the desired results. See image below:
Desired Functionality
When I tie the functionality into my single page application using the same code I get a sort of blocking issue where all requests are responded to only after the last task has completed. In the test app above all request are responded to in order. The server reports a ("pending") state for all requests until the controller method has completed. Can anyone give me a hint as to what could cause the change in behavior?
Not Desired
Desired Fiddler Request/Response
GET http://localhost:12028/task/status?_=1383333945335 HTTP/1.1
X-ProgressBar-TaskId: 892183768
Accept: */*
X-Requested-With: XMLHttpRequest
Referer: http://localhost:12028/
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Connection: Keep-Alive
DNT: 1
Host: localhost:12028
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcVEVNUFxQcm9ncmVzc0Jhclx0YXNrXHN0YXR1cw==?=
X-Powered-By: ASP.NET
Date: Fri, 01 Nov 2013 21:39:08 GMT
Content-Length: 25
Iteration completed...
Not Desired Fiddler Request/Response
GET http://localhost:60171/_Test/status?_=1383341766884 HTTP/1.1
X-ProgressBar-TaskId: 838217998
Accept: */*
X-Requested-With: XMLHttpRequest
Referer: http://localhost:60171/Report/Index
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Connection: Keep-Alive
DNT: 1
Host: localhost:60171
Pragma: no-cache
Cookie: ASP.NET_SessionId=rjli2jb0wyjrgxjqjsicdhdi; AspxAutoDetectCookieSupport=1; TTREPORTS_1_0=CC2A501EF499F9F...; __RequestVerificationToken=6klOoK6lSXR51zCVaDNhuaF6Blual0l8_JH1QTW9W6L-3LroNbyi6WvN6qiqv-PjqpCy7oEmNnAd9s0UONASmBQhUu8aechFYq7EXKzu7WSybObivq46djrE1lvkm6hNXgeLNLYmV0ORmGJeLWDyvA2
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcUHJvamVjdHNcSUxlYXJuLlJlcG9ydHMuV2ViXHRydW5rXElMZWFybi5SZXBvcnRzLldlYlxfVGVzdFxzdGF0dXM=?=
X-Powered-By: ASP.NET
Date: Fri, 01 Nov 2013 21:37:48 GMT
Content-Length: 25
Iteration completed...
The only difference in the two requests headers besides the auth tokens is "Pragma: no-cache" in the request and the asp.net version in the response.
Thanks
Update - Code posted (I probably need to indicate this code originated from an article by Dino Esposito )
var ilProgressWorker = function () {
var that = {};
that._xhr = null;
that._taskId = 0;
that._timerId = 0;
that._progressUrl = "";
that._abortUrl = "";
that._interval = 500;
that._userDefinedProgressCallback = null;
that._taskCompletedCallback = null;
that._taskAbortedCallback = null;
that.createTaskId = function () {
var _minNumber = 100,
_maxNumber = 1000000000;
return _minNumber + Math.floor(Math.random() * _maxNumber);
};
// Set progress callback
that.callback = function (userCallback, completedCallback, abortedCallback) {
that._userDefinedProgressCallback = userCallback;
that._taskCompletedCallback = completedCallback;
that._taskAbortedCallback = abortedCallback;
return this;
};
// Set frequency of refresh
that.setInterval = function (interval) {
that._interval = interval;
return this;
};
// Abort the operation
that.abort = function () {
// if (_xhr !== null)
// _xhr.abort();
if (that._abortUrl != null && that._abortUrl != "") {
$.ajax({
url: that._abortUrl,
cache: false,
headers: { 'X-ProgressBar-TaskId': that._taskId }
});
}
};
// INTERNAL FUNCTION
that._internalProgressCallback = function () {
that._timerId = window.setTimeout(that._internalProgressCallback, that._interval);
$.ajax({
url: that._progressUrl,
cache: false,
headers: { 'X-ProgressBar-TaskId': that._taskId },
success: function (status) {
if (that._userDefinedProgressCallback != null)
that._userDefinedProgressCallback(status);
},
complete: function (data) {
var i=0;
},
});
};
// Invoke the URL and monitor its progress
that.start = function (url, progressUrl, abortUrl) {
that._taskId = that.createTaskId();
that._progressUrl = progressUrl;
that._abortUrl = abortUrl;
// Place the Ajax call
_xhr = $.ajax({
url: url,
cache: false,
headers: { 'X-ProgressBar-TaskId': that._taskId },
complete: function () {
if (_xhr.status != 0) return;
if (that._taskAbortedCallback != null)
that._taskAbortedCallback();
that.end();
},
success: function (data) {
if (that._taskCompletedCallback != null)
that._taskCompletedCallback(data);
that.end();
}
});
// Start the progress callback (if any)
if (that._userDefinedProgressCallback == null || that._progressUrl === "")
return this;
that._timerId = window.setTimeout(that._internalProgressCallback, that._interval);
};
// Finalize the task
that.end = function () {
that._taskId = 0;
window.clearTimeout(that._timerId);
}
return that;
};
Update 1 - Many thanks to John Saunders. I was able to locate this article that explains what John was implying in his comment about serialized access
"It blocks parallel execution and forces parallel requests to be executed one after another because the access to ASP.NET Session state is exclusive per session"
I found a fix at last. The session state can be controlled at the Controller and/or Controller Method level. Since the authorization is being verified at a higher level there is no need to use session in what I am doing. I simply disable it for the unit of work.
[SessionState(SessionStateBehavior.Disabled)]
public class _TestController : ProgressWorkerController