Using AJAX to invoke the Web API, the URL is too long to solve the problem;

Source: Internet
Author: User

WCF would have been used, but the service would need to be called in multiple ways (background + front-end Ajax), and the final situation was that I judged it in every WCF service ↓

            #regionResolves a parameter that was not received in Ajax ...if(Jsonparames = =NULL&& httpcontext.current.request.querystring["Parameters"] !=NULL) Jsonparames= httpcontext.current.request.querystring["Parameters"]; Else if(Jsonparames = =NULL&& httpcontext.current.request.querystring["Parameters"] ==NULL)                returnJson. Instance.tojson ("{\ "ret\": \ "0\", \ "msg\": \ "parameter is empty. \"}"); #endregion

But there is a length limit to using QueryString to transmit data in Ajax.

And someone said they could use a form to resolve the submission.

<form id='Form0'Method='POST'action='http://localhost:22377/api/Article_/Article_Update/'onsubmit ="set_v ()"><input id='Id_v'Type='Hidden'Value='Value_default'Name='Jsonparames'/><input type="Submit"Value="XXX" class=""/></form>

However, after the submission is finished, the page will be directly redirected to the page where action is directed ...

Finally, decide to publish a Web API, and then use Ajax to solve

<form id= "Form1" > <div>ID:<input type= "text" id= "id_id"/> <br/> <input type= "button" value= "POST" id= "getpers Ons "/> </div> <div id=" ret "> </div> </form> <script type=" text/j Avascript ">functionSet_v () {alert (' Set value! '); varv = ' too long data. '; document.getElementById ("Id_v"). Value =v; Alert (document.getElementById ("Id_v"). Val ()); }$ (' #getPersons '). Click (function() {document.getElementById ("ret"). InnerHTML = ';//clear last query content ...            varID = $ ("#id_ID"). Val ();$.ajax ({type:' POST ',URL: ' Http://localhost:22377/api/Article_/Article_Update ',//? jsonparames= ' + Jsonparames,                //dataType: ' JSONP ',//If the line does not annotate the requested ' type ' is get, (even if the first line specifies the type: ' POST ')                //contentType: "Application/json; Charset=utf-8; ",//This line does not comment that cross-domain calls are not allowed!!!data: {"Jsonparames": ' {"id": "0"} '//jsonparames corresponds to the parameter name in the Web API;                },Successfunction(_data) {alert (_data); varinfo = eval (' (' + _data + ') '); //alert (data);                    varList = eval (' (' + info.list + ') '); varFragment =document.createdocumentfragment (); $.each (list,function(i, field_list) {$.each (field_list,function(i, field) {varItem = document.createelement ("li"); Item.appendchild (document.createTextNode (' [' + i + '] ' + ' ... ' +field));                            Fragment.appendchild (item); //Alert (i + "... is ..." + field);                        })                    })                    $("#ret"). append (fragment); }, Error:function () { }            });    }); </script>

the Web API;

[HttpPost, HttpGet] Public stringArticle_update ([Frombody]stringjsonparames) {            if(string. IsNullOrEmpty (jsonparames) | | Jsonparames.tolower () = ="Jsonparames") {Jsonparames= system.web.httpcontext.current.request.form["Jsonparames"]; }            return "xx";}

Using AJAX to invoke the Web API, the URL is too long to solve the problem;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.