Mvc5 WEBAP2 How the foreground uses Ajax to request the background API

Source: Internet
Author: User

According to the normal wording, there are always 404 errors, studied for a long time, found here the solution: Http://buxuxiao.com/article/using-jquery-to-post-frombody-parameters-to-web-api

Now summarize,

In case of a single parameter:

1, the background parameters of the correct wording as follows:

        [Route ("services/{controller}/{action}")][httppost][httpget]  Public string Uploadtransportnetworkalarmchat ([frombody]string  obj) {
...}

You must precede the parameter list with the [Formbody] label.

2, the parameters of the foreground, still added in Key-value mode, but the key name must be set to null:

$.ajax ({                "POST",                ".. /services/chattest/uploadtransportnetworkalarmchat ",                data: {" ":" HelloWorld "},                ' JSON ' ,})

Multiple parameters in the case,

1, the background needs to define the entity class, the parameter list does not add the [Frombody] label, as follows:

     Public class Tempobj    {        publicstringgetset;}          Public string Get Set ; }    }

Receive method:

[Route ("services/{controller}/{action}")]        [HttpPost]        [HttpGet]          Public string uploadtransportnetworkalarmchat (tempobj obj)        {}

2. The foreground still uses JSON

            var Tempobj = {"ImgBase64": Image, "name": "HelloWorld" }            $.ajax ({                "POST",                "..") /services/chattest/uploadtransportnetworkalarmchat ",                data:tempobj,                ' JSON ',            })              . Done (function  (data) {                  alert ("Data Loaded:" + data);              });

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.