❔ how to pass multiple arguments in apicall from ajex get url?

i used following approach already
49 Replies
Angius
Angius2y ago
Copied from #web
mer_nam hai bulla rakhta hu kula
ok let me try some none of these work
mer_nam hai bulla rakhta hu kula
by the way what is that question mark can you explain me
Angius
Angius2y ago
Ah, that's for passing query parameters But I see now you're using route parameters Yeah, I don't think you can do it this way, you need query params
mer_nam hai bulla rakhta hu kula
how can i create one
Angius
Angius2y ago
ASP Core should handle the ?foo=1&foo=2&foo=3 way, IIRC
Angius
Angius2y ago
With something like
[HttpGet]
public async Task<ActionResult> OnGet([FromQuery] int[] foo)
{
// ...
}
[HttpGet]
public async Task<ActionResult> OnGet([FromQuery] int[] foo)
{
// ...
}
mer_nam hai bulla rakhta hu kula
what you are trying to explain
Angius
Angius2y ago
I'm trying to explain how to pass an array of values as a query parameter...?
mer_nam hai bulla rakhta hu kula
hello i have modified my api according to it and now it is like this
mer_nam hai bulla rakhta hu kula
but then how should i modifying this xhr.open('GET', https://localhost:44385/Exam/GetExamResultByAdmin/${subId}&${Uid}, true);
Angius
Angius2y ago
xhr.open('GET', `https://localhost:44385/Exam/GetExamResultByAdmin?id=${subId}&id=${Uid}`, true);
xhr.open('GET', `https://localhost:44385/Exam/GetExamResultByAdmin?id=${subId}&id=${Uid}`, true);
But I have to wonder... you're sending two values Why use an array at all?
mer_nam hai bulla rakhta hu kula
brah you are the one who suggested see
Angius
Angius2y ago
Why not just do
xhr.open('GET', `https://localhost:44385/Exam/GetExamResultByAdmin/${subId}&${Uid}`, true);
xhr.open('GET', `https://localhost:44385/Exam/GetExamResultByAdmin/${subId}&${Uid}`, true);
as you did, and have the method on the API side be
[HttpGet("{id}/{uid}")]
public async Task<...> OnGet(int id, int uid)
{

}
[HttpGet("{id}/{uid}")]
public async Task<...> OnGet(int id, int uid)
{

}
Ah, I thought you wanted to send an array of values If you want to pass multiple values, just use route params
mer_nam hai bulla rakhta hu kula
but agin how do i config my xhr.open() so it can send two vals
MODiX
MODiX2y ago
Angius#1586
Why not just do
xhr.open('GET', `https://localhost:44385/Exam/GetExamResultByAdmin/${subId}&${Uid}`, true);
xhr.open('GET', `https://localhost:44385/Exam/GetExamResultByAdmin/${subId}&${Uid}`, true);
as you did, and have the method on the API side be
[HttpGet("{id}/{uid}")]
public async Task<...> OnGet(int id, int uid)
{

}
[HttpGet("{id}/{uid}")]
public async Task<...> OnGet(int id, int uid)
{

}
React with ❌ to remove this embed.
Angius
Angius2y ago
It's positional (also, don't use xhr, use fetch)
const data = await fetch(`/things/${id}/${slug}`);
const data = await fetch(`/things/${id}/${slug}`);
[Route("[controller]")]
public class ThingsController : ControllerBase
{
[HttpGet("{id}/{slug}")]
public async Task<IActionResult<Thing>> GetThing(int id, string slug)
{
return await _context.Things
.Where(t => t.Id == id && t.Slug == slug)
.FirstOrDefaultAsync();
}
}
[Route("[controller]")]
public class ThingsController : ControllerBase
{
[HttpGet("{id}/{slug}")]
public async Task<IActionResult<Thing>> GetThing(int id, string slug)
{
return await _context.Things
.Where(t => t.Id == id && t.Slug == slug)
.FirstOrDefaultAsync();
}
}
Here's the gist of it
Angius
Angius2y ago
Where am I using ??
mer_nam hai bulla rakhta hu kula
nahh bro none of those approch worked so got just a little bit creative
Angius
Angius2y ago
Show your code
mer_nam hai bulla rakhta hu kula
dev-vc-0 join
Angius
Angius2y ago
Just send the code $paste if it's too much
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Angius
Angius2y ago
Or link a Github repo
mer_nam hai bulla rakhta hu kula
ok git sounds good to me
mer_nam hai bulla rakhta hu kula
GitHub
GitHub - digvijay-ship-it/Online-Exam
Contribute to digvijay-ship-it/Online-Exam development by creating an account on GitHub.
Angius
Angius2y ago
And this code https://github.com/digvijay-ship-it/Online-Exam/blob/main/Online%20Exam%20Web/wwwroot/js/attemptedExamAdmin.js#L105 is supposed to call this https://github.com/digvijay-ship-it/Online-Exam/blob/main/Online%20Exam%20Web/Controllers/UserController.cs#L63 ? If so, then you haven't read any of the code I sent So read it again The part about [HttpGet] and the route params defined there And how the URL looks
mer_nam hai bulla rakhta hu kula
yes bro i went to grab my lunch i thought you would take some time to go through the project son in mean time i thought i can have my lunch wasn't expecting you to reply this fast
Angius
Angius2y ago
I just found the two places of importance, no need to go through the whole project In any case
MODiX
MODiX2y ago
Angius#1586
const data = await fetch(`/things/${id}/${slug}`);
const data = await fetch(`/things/${id}/${slug}`);
[Route("[controller]")]
public class ThingsController : ControllerBase
{
[HttpGet("{id}/{slug}")]
public async Task<IActionResult<Thing>> GetThing(int id, string slug)
{
return await _context.Things
.Where(t => t.Id == id && t.Slug == slug)
.FirstOrDefaultAsync();
}
}
[Route("[controller]")]
public class ThingsController : ControllerBase
{
[HttpGet("{id}/{slug}")]
public async Task<IActionResult<Thing>> GetThing(int id, string slug)
{
return await _context.Things
.Where(t => t.Id == id && t.Slug == slug)
.FirstOrDefaultAsync();
}
}
React with ❌ to remove this embed.
Angius
Angius2y ago
Take a look at this again. At the url and the attribute And see how they differ from what you have
mer_nam hai bulla rakhta hu kula
bro is there a way i can use this without fetch i mean i am new to js so want take it slow so cant we do that with Xhr object?
Angius
Angius2y ago
Sure can I just used fetch as an example The url is what matters And you should use fetch especially if you're new tbh, xhr is much more complex for no reason Stick to xhr if you want, tho
mer_nam hai bulla rakhta hu kula
that why i like to use it you see when there are lot megic going around there is less scope of learning
Angius
Angius2y ago
Eh, I guess Just be advised, that in real life you're not gonna be using xhr Unless working on some 1999 legacy project that needs to work on Netscape Navigator
mer_nam hai bulla rakhta hu kula
ok i will use it this one time then i will use fetch api from onwards
Angius
Angius2y ago
Sounds reasonable, yeah
mer_nam hai bulla rakhta hu kula
ok i changed url and function in controller
mer_nam hai bulla rakhta hu kula
but now the system is throwing me an error
mer_nam hai bulla rakhta hu kula
after applying this it is throwing this
mer_nam hai bulla rakhta hu kula
i tried removing it and then this error stops
Angius
Angius2y ago
Do you route to the controller in some special way? Or do you just let convention take care of it? If it's the latter, then the name of the method is not a part of the url
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server