There is no simple brain with violent Ajax. Get access API

Source: Internet
Author: User

Write the interface, just to let the mobile side, Web side, or PC client call.

We write an interface that returns the JSON format, the function is about arithmetic.

<?php
Header (' Content-type:application/json ');
$operator = $_get[' operator ');
$first = $_get[' first ');
$second = $_get[' second ');
if ($operator = = ' Jia ') {//addition
$result = $first + $second;
}
else if ($operator = = ' Jian ') {//subtraction
$result = $first-$second;
}
else if ($operator = = ' Cheng ') {//multiplication
$result = $first * $second;
}
else{//Division
$result = $first/$second;
}
$result = Array (' res ' = = $result, ' status ' =>1);
echo Json_encode ($result);//return JSON data format
?>




<! DOCTYPE html>
<meta charset= ' Utf-8 ' >
<script></script>
<center>
<input type= ' text ' name= ' first ' id= ' first ' >
<input type= ' text ' name= ' second ' id= ' second ' >
<button value= ' Jia ' > Additions </button>
<div id= ' Jia ' ></div>
<button value= ' jian ' > Subtraction </button>
<div id= ' Jian ' ></div>
<button value= ' Cheng ' > multiplication </button>
<div id= ' Cheng ' ></div>
<button value= ' chu ' > Division </button>
<div id= ' Chu ' ></div>
</center>
<script src= ' jquery.js ' ></script>
<script>
$ (function () {
$ (' button '). Click (function () {
var first = $ (' #first '). Val ();
var second = $ (' #second '). Val ();
var operator = $ (this). Val ();
var area = operator;
var query = ' http://localhost/api.php/? ' + ' first= ' +first+ ' &second= ' +second+ ' &operator= ' +operator;
$.get (query, ", function (data,st) {$.get (We want to enter the Urlget string, the parameters are left blank, callback)
$ (' # ' +area). Text (data.res); The button to which the operation is pressed, it is necessary to assign a value in that area
});
});
});
</script>

There is no simple brain with violent Ajax. Get access API

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.