asp.net mvc 3 - Posting .net MVC 3 FromsCollection from other domain with jquery ajax -
ok, i'm totally new .net mvc3 , i've run problem.
i developing mobile application using jquery mobile , wish send data mobile application web-page. on server have this:
[httppost] [validateinput(true)] public actionresult save(formcollection actionvalues) { int age = int32.parse(actionvalues["age"]); string fn = actionvalues["first_name"]; string ln = actionvalues["last_name"]; createandstorepersonmodel(age,fn,ln); // dummy method, not important return new httpstatuscoderesult(200); // 3nigma }
what want able actionvalues , store them in model, store model database. sake of example we'll assume want store "person" attributes: "first_name, last_name, age". might expand model in future.
from mobile app run following code:
$.ajax({ type: "post", url: "http://external.url/save", datatype: "json", traditional: true, // default serialization (do need this?) data: { "age": data_age, "first_name": data_fn, "last_name": data_ln, }, success: function(d) { alert("success: "+d}, }).error(function(data, errortxt, jqxhr) { alert('error: '+errortxt); });;
i got 500 internal error, 3nigma no longer case.
edit:
when testing webserver http 302 "found" when checking inspector, data not saved. when compiling mobile phone .error handler kicks inn "parseerror", data gets saved. idea why?
answer:
the 302 "found" came because returned view (thanks 3nigma) should return this:
return new httpstatuscoderesult(200);
500 internal server error
public actionresult save(formcollection actionvalues) { int age = long.parse(actionvalues["age"]);// there error seems here boxing long int string fn = actionvalues["first_name"]; string ln = actionvalues["last_name"]; createandstorepersonmodel(age,fn,ln); }
try instead
int age = int32.parse(actionvalues["age"].tostring());
answering comments
you dont need include return json actionresult
[httppost] public actionresult save(formcollection actionvalues) { //your code here return json(new {issuccess="success" }); }
and in ajax success handler can access
$.ajax({ type: "post", url: "http://external.url/save", datatype: "json", traditional: true, // default serialization (do need this?) data: { "age": data_age, "first_name": data_fn, "last_name": data_ln, }, success: function(data) { alert("success: "+data.issuccess}, //will alert success: success }).error(function(data, errortxt, jqxhr) { alert('error: '+errortxt); });;
Comments
Post a Comment