Posting Multipart and string content web API using Ajax in Asp.net - c#

I have multipart content web API. which is working fine.and tested in postman.
Now i am trying to consume that web api in my asp.net web application using ajax post method. I referred some coding in google and applied. but it shows error finally. i don't know what's the misake i am doing. I am using .Net Framework 4.5. My coding and postman execution of my api as Below .
var data = new FormData();
jQuery.each(jQuery('#fileupload')[0].files, function (i, file) {
data.append('file', file);
});
data.append('siteCode', 'HQ');
data.append('phoneNumber', '95878784XXX');
data.append('customerCode', 'C001');
data.append('notificationFlag', '1');
$.ajax({
type: 'POST',
url: SequoiaUri + "api/profilePicture",
contentType: "application/json; charset=utf-8",
data: data,
dataType: "json",
Cache: false,
processData: false,
success: function (data) {
alert("Picture Uploaded Successfully!");
}
Can anyone help me what's the error in my ajax post method...

try this:
$.ajax({
url: SequoiaUri + "/api/profilePicture",
data: data,
type: 'POST',
//enctype: 'multipart/form-data', // try if still is not working
contentType: false,
processData: false,
success: function (data) {
alert("Picture Uploaded Successfully!");
},
.....

Related

How to know client URL name?

I have created web-api to provide service like pin-code, Bank IFSC Code and so on, from my website named as http://www.ajaxserver.com
My all api is hosted on my site and my all client access using my site.
web api code is
[Route("api/GetURLName/")]
[HttpGet]
public string GetURLName(HttpRequestMessage request)
{
return HttpContext.Current.Request.Url.AbsoluteUri ;
}
One of my client website name is http://www.clientwebsite.online
client is used jquery to retrieve info like below code.
$('#btnTestCore').click(function () {
$.ajax({
url: 'http://ajaxserver.com/api/GetURLName/',
dataType: 'json',
type: 'GET',
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
},
error: function (data) {
alert('failed.');
}
});
});
Output comes:
"http://ajaxserver.com/api/GetURLName/".
Need Output:
"http://clientwebsite.online/api/GetURLName/"
You could append the output to your AJAX request:
$('#btnTestCore').click(function () {
var urlname = encodeURI("http://clientwebsite.online/api/GetURLName/")
$.ajax({
url: 'http://ajaxserver.com/api/GetURLName?urlname=' + urlname,
dataType: 'json',
type: 'GET',
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
},
error: function (data) {
alert('failed.');
}
});
});
You will then, of course, have to read that querystring in your webAPI code
As far as the server of the ajax request knows; the "site" is itself, so http://www.ajaxserver.com. You could try to look at the referrer, but... that isn't reliable across all browsers and protocols.
So you have two choices:
have the client API tell you the calling site - note that it could trivially be spoofed by the client
do some non-trivial proxy (etc) configuration such that the ajax server's site responds on the client domain
Note that http://clientwebsite.online/api/GetURLName/ was not the actual URL and is not going to come from anywhere by itself.

how to set caching for ajax request and response

I'm working with ajax request and I received JSON object as a response from AJAX.
Now I'm trying to cache that JSON object and how to get data from cache.I want to show the cache data from next request to the server with same request with same parameter values.
self.categorySelected = function (selectedCategory, event) {
$.ajax({
url: '/services/ShopService.asmx/XGetRefurbishedproducts',
data: JSON.stringify({ displayName: itemDisplayName, categoryid: selectedCategory.CategoryId(), language: lang }),
type: "POST",
dataType: "json",
cache: true,
contentType: "application/json; charset=utf-8",
beforeSend: function () { $(event.target).prop('disabled', true); }
}).done(function (data) {
//sample
})
}

jQuery Calls to .NET WebMethod Working Out of Order

I am using lot of .NET WebMethod calls in my web application using jQuery. My calls are not working in the expected order. The first time the page loads it works fine, and after reloading the page, the WebMethod calls are disordered. How do I make it always work one by one in the expected order?
I am using jQuery $.ajax():
$.ajax({
type: "POST",
url: "",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) { },
error: function (msg) { alert(msg.error); }
});
Add async: false option in ajax. By default Ajax make async call hence order is not guaranteed.
$.ajax({
type: "POST",
async : false, // Add this
url: "",
data: '',
contentType: "application/json;
charset=utf-8",
dataType: "json",
success: function (response) { },
error: function (msg) { alert(msg.error); }
});

ASP.NET + jQuery AJAX - getting 500 server error

I want to call an ASP.NET function from jQuery by AJAX with response.
I have file Controll.aspx where is included javascript code. Next I have /Services/ControllService.asmx, where is the function, which I want call from js.
js code:
$(document).ready(function () {
$('#btn_start').on('click', function () {
$.ajax({
type: "POST",
url: "Services/ControllService.asmx/Start",
data: {},
dataType: "json",
async: true,
contentType: "application/json; charset=utf-8",
success: function (response) {
console.log(response);
},
error: function (err) {
alert("Error:" + err.toString());
}
});
});
});
But I still getting the error 500.
POST http://localhost:56000/Services/ControllService.asmx/Start 500 (Internal Server Error)
k.cors.a.crossDomain.send
n.extend.ajax
Do you have any hints, what do I need to set e.g. in Web.config?
Many thanks.
SOLVED:
1 - I have defined Start function in ControllService.asmx.cs as static.
2 - I have badly configured data. It has to be named by the same way e.g. "sth".
In javascript it should be:
...
url: "Services/ControllService.asmx/Start",
data: JSON.stringify({ sth: "hahaha" }),
dataType: "json",
...
and in ControllService.asmx.cs -> method Start
public string Start(string sth){}
Many, many thanks for your hints.

Ajax URL - Incorrect .net #c

Been following some tutorials, as i'm learning C#, however was just attempting to make an ajax call to the server, i'm using my localhost.
As far as I can tell i'm doing its right, but it's obviously not.
Maybe it my folder structure or just the name of the file.
The Default.aspx file is at the project root
Here is my ajax call
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx",
dataType: "json",
data: "{'FirstName':'test','LastName':'test1','City':'test3','EmailID':'test4'}",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});
I know eventually it will have to call a method within the file, but its not even finding it at the moment.
Thanks in advance, James
You can use controller Home and create the one action.
Example:
public ActionResult FirstAjax()
{
return Json(true, JsonRequestBehavior.AllowGet);
}
After that your jquery ajax is:
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "Home/FirstAjax",
dataType: "json",
data: "",
success: function (data) {
console.log("Woo!");
},
error: function (result) {
console.log("fail!");
}
});

Categories