Reputation: 1011
Can anyone see anything i m doing wrong with this as its driving me a bit mad (not sure if it friday brain or baby brain) anyway...
I have a Ajax post which works fine locally but doesnt work when i deploy to another server.
I've put lots of alerts in to make sure I'm getting the parameters that i expect so not quite sure whats missing.
Here is the ajax post:
View:
var theUrl = "/Widgets/TestBin2AutomationResults/" + widgetImpressionId + "/" + retailerProductId + "/" + quantity;
alert("widgetImpressionId" + widgetImpressionId);
alert("retailerProductId" + retailerProductId);
alert("quantity" + quantity);
alert(theUrl);
$("#imgaddtocart").hide();
$("#addMe").unbind('click');
$("#delete").unbind('click');
$.ajax({
type: "POST",
url: theUrl,
data: { 'username': username, 'password': password },
dataType: "json",
success: function (data) {
if (data != null) {
alert("we are inside data");
Controller:
[JsonpFilter]
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult TestBin2AutomationResults(int widgetImpressionId, int retailerProductId, int quantity, string username, string password)
{
MessageBox.Show("We are inside TestBin2Automation controller " + widgetImpressionId + "/" + retailerProductId + "/" + quantity + "/" + username + "/" + password);
Global.asax
routes.MapRoute("Bin2SubmitTestBin2Automation", "Widgets/TestBin2AutomationResults/{widgetImpressionId}/{retailerProductId}/{quantity}", new { controller = "Widgets", action = "TestBin2AutomationResults", widgetImpressionId = 0, retailerProductId = 0, quantity = 0, username = "", password = "" });
I'm not getting into the controller as the MessageBox.Show isn't displaying.
Any help appreciated it d be nice to not have this hanging over my head all weekend!
Many thanks
Upvotes: 3
Views: 5429
Reputation: 393
i am also face this issue in my mvc project ....
i am solved this issue by following steps.
1) Put this in your Layout page in script section
<script type="text/javascript">
var RootUrl = '@Url.Content("~/")';
</script>
2) add the "RootUrl" variable in ajax url. (it's also working your Js File you add "RootUrl" before your ajax url)
var theUrl = RootUrl +"Widgets/TestBin2AutomationResults/" + widgetImpressionId + "/" + retailerProductId + "/" + quantity;
It's working prefect for me but anyone have other solution then plz post me
Upvotes: 3
Reputation: 1011
it was actually that i was trying to access Amazon AWS in the controller but had not put in the Access keys into the config and i thought that it wasn t getting into that controller as it wasn t hitting the first line but when i removed pretty much all the logic out of the controller it entered the controller fine so it wasn t the ajax call at all but rather the logic inside - i just thought as it wasn t getting to the first line it wasn t getting in there which wasn t the case. So stripping the controller down to be very basic let me know what the actual issue was.
Upvotes: -1
Reputation: 356
your URL is started with "/" it means it will try to resolve URL from root of domain. Please verify your application is deployed in root of domain or under any virtual directory. If it is deployed under virtual directory than you need to change JavaScript code to include virtual directory while making AJAX call.
Upvotes: 0
Reputation: 388
Since the ajax post works locally,try and enable CORS on the live web server. Cross-origin domain is the likely reason behind the problem you're having. What are using on your backend? Is it PHP, ASP.NET or Python?
Upvotes: 0