0

I have one problem with jQuery.post() syntax.

I have the controller ExampleController.cs, with an action:

public ActionResult TestPost(Guid fileId) { //do something }

and the view Example.cshtml, and here I attempt to load this action:

function foo(fileGuid) {      
    $.post("Example/TestPost?fileId=" + fileGuid, function () {
        alert("success");
    });
}

But there's no alert, I think something wrong with this syntax. Can anyone help me out. Thanks.

4 Answers 4

1

Try adding the following 3 callbacks and see if you get an error alert,

var jqxhr = $.post("example.php", function() {
   alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks. I did as you suggested. I got 2 alerts error and complete, the error was example.php was not found. it's not php btw.
@QuiTran please accept the answer and close the question from further discussion.
1

You should also check the Networks option in the console of Chrome and see if there is any error in server side. That can also be a reason why function may have failed.

2 Comments

I checked it, and it's said POST TestPost?fileId=blahblah 404 Not Found. I suppose that it's failed to load data from server ???
I think then you should check if you are passing the correct url from the ajax function.
0

I think you need to add a backslash on your location:

$.post("/Example...

If that does not work, try annotating your method with a post annotation:

[HttpPost]
public ActionResult TestPost(Guid fileId) { //do something }

Edit:

It is not finding your action because you are specifying the parameter as a Guid. If you change this to a string:

 public ActionResult TestPost(string fileId)
 {
       return View();
 }

and run:

$.post('/Example/TestPost?fileId=123', function () {
  alert('here');
});

It should work

Comments

0

Ok, I found this way:

$.post('@Url.Action("TestPost", "Example")', {fileId : fileGuid} , function () {
        alert("success");
    });

and it works. :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.