Formdata is null on backend (ajax, react, asp.net)? - ajax

I went through countless similar questions but I just don't get it.
It seems that the data I send is correct but the backend recieves empty formData.
[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(IFormFile formFile)
{
//logic throws nullreference at formFile
}
Ajax:
console.log($('#fileTest')[0].files[0]);
var fd = new FormData();
fd.append('files', $('#fileTest')[0].files[0]);
$.ajax({
url: 'https://localhost:44348/api/user/UploadFiles',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);},
error: function (jQXHR, textStatus, errorThrown) {
console.log("An error occurred whilst trying to contact the server: " + jQXHR.status + " " + textStatus + " " + errorThrown);
}
The console.log result:
So I'm completly stuck. Can anyone suggest something I can try? Thank you very much in advance.

Related

How do you make an Ajax call to Google Drive API?

My code can get an auth2.0 token...but the examples I've found don't show the final piece of taking a GET, provided here, and turning it into a working request.
Here is the specific URL from that link:
GET https://www.googleapis.com/drive/v3/files/fileId
Here is my AJAX call with the attempt at getting the token (that works) into a hardcoded fileId that I know my Google account has access to.
$.ajax({
type: "GET",
beforeSend: function(request) {
request.setRequestHeader("Authorization", "Bearer" + token);
},
// url: "https://www.googleapis.com/drive/v2/files/18qxc3YgnQ_Yg8n4Q18WCZahE9EPtOZWhoKJuAx6SEHI/permissions",
url: "https://www.googleapis.com/drive/v3/files/18qxc3YgnQ_Yg8n4Q18WCZahE9EPtOZWhoKJuAx6SEHI",
dataType: 'application/json',
processData: true,
success: function(msg) {
console.log('Got File Metadata: ' + msg) + console.log(msg);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('Error: ' + errorThrown + ' / ' + textStatus) + console.log(jqXHR);
}
});
I don't understand how to include the token and the pages and pages of Google documentation do not include any examples of going this route.
I get "invalid credential" errors in the server response.
I don't want to use GAPI because I ran into invalid cookie issues running locally and my code is in a Chrome Extension and it isn't clear whether that approach will work there.
Thank you for any help or direction.
In your code, this line:
request.setRequestHeader("Authorization", "Bearer" + token);
You should have a space after Bearer
request.setRequestHeader("Authorization", "Bearer " + token);

Web Core API Post request get blocked, sometimes

I have this AJAX call to my Web Core API;
saveProperty = function (url, data, callback, errorCallback) {
$.ajax({
async: true,
type: "Post",
url: url,
data: data,
contentType: "application/json",
dataType: "text"
})
.done(callback)
//.fail(errorCallback);
.fail(function (jqXHR, textStatus, errorThrown) {
alert("Something went wrong: " + textStatus + " " + errorThrown);
errorCallback();
});
}
Sometimes it works, more often it does not.
When I use Fiddler and it works, I see
And when it doesn't I see;
EDIT - I can be more helpful now in describing this problem.
if I have Fiddler up and running before I lanch the aplication in development, I do not get this bug.
Otherwise I do get this bug.
Why is that? I would love to know.
The error is HTTP Error 401.2 - Unauthorized
Why would this happen with the same code?

Value cannot be null. Parameter name: value, while calling httppost via ajax post

I am trying to receive a post request with some json string
var contentType ="application/x-www-form-urlencoded; charset=utf-8";
$.ajax({
type: "POST",
data: JSON.stringify(fileData),
url: "http://hercules/JsonTest/api/Getjson/sendData",
contentType: contentType,
processData: false,
error: function (qXHR, textStatus, errorThrown) {
alert("failure: " + qXHR.status + ":" + textStatus + errorThrown);
},
success : function(data) {
alert("Success : " + data);
}
});
and in my webapi
[System.Web.Http.HttpPost]
[System.Web.Http.ActionName("sendData")]
public string sendData([FromBody] string jsonString)
{
logData("Entering SendData : " + jsonString);
return "Success";
}
But, my jsonString posted is not received in the webapi. it is throwing error Value cannot be null.
Parameter name: value
What could be the reason.
I was testing the code directly in my visual studio and the same was working fine. but once i published the same in machine, and tried to access via a jsFiddle, then the string is showing empty.
I got the issue resolved.
In my ajax post request i modified
data: JSON.stringify(fileData),
to
data: '=' + fileData,
With equal to added, the data is correctly received in HTTPPOST method of MVC.
Thanks...

jquery ajax call in UserControl asp.net returns error "Internal Server Error"

I have a UserControl (.ascx) and I want to do an Ajax control on the email.
this is my ajax call inside the file Login.ascx
$.ajax({
url: "Login.aspx/CheckEmail",
type: 'POST',
data: "{email:'"+email+"'}",
dataType: "json",
success: function (data) {
alert("We returned: " + data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
}
});
and this is my webMethod in file Login.aspx.cs
[WebMethod]
public static string CheckEmail(string email)
{
return "it worked";
}
Now, I always have the Error message that is telling me: Internal Server Error
What is wrong here?
thanks
You can't place the WebMethod inside user controls, you have to place it inside a page or inside a service.
Hope this helps.

How to post like with ajax to instagram? Help me please

i have this code but its not working. it says to me ajax post is success but when i look make refresh i saw its not liked.
<?
if ($data->user_has_liked == false)
{
?>
<span class="<?=$data->id;?>">Like</span>
<? } else { echo 'Liked'; } ?>
<script type="text/javascript">
$('a.like').click(function() {
var mediaId = $(this).attr('id');
$.ajax({
url: "https://api.instagram.com/v1/media/" + mediaId + "/likes?callback=?",
dataType: "jsonp",
data: {
access_token: '<?=$access_token;?>',
_method: 'POST'
},
type: "POST",
success: function(data, textStatus, jqXHR) {
$("."+mediaId+"").text('Liked');
},
error: function(jqXHR, textStatus, errorThrown) {
$("."+mediaId+"").text('Error!<br/>' + textStatus + ' - ' + errorThrown);
}
});
});
</script>
I am building something similar and have ran into this problem recently.
Even though your request is sent as a type: POST the request itself is actually sent as GET. This causes your request to return successful without liking anything. Instead it will return all the data for that Media ID.
If you change your datatype to JSON instead of JSONP you request should work as you wish; however, it may require an HTTPS connection requiring an SSL.

Resources