Use Ajax to execute the GP Service
Overview:
In the previous article (Arcgis for js GP implements buffer computing), we introduced Arcgis for js to implement buffer computing. However, many times, we don't want to use Arcgis but want to call GP to implement the corresponding functions. How can we do this? Yes, this article will explain how to execute GP and return results through ajax.
Ideas:
Press F12 to open Google's debugging tool, switch to the network, and then execute it. You can see the url requested during GP execution, as shown below:
Open 1, 2, and 3 respectively in the browser. The request results are as follows:
1,
{jobId:j75ee9938cd9b450fb76eb0f603040fbc,jobStatus:esriJobSubmitted}
2,
{jobId:j123084f770754cf6b76892d2fa44b80e,jobStatus:esriJobSucceeded,results:{output:{paramUrl:results/output}},inputs:{input:{paramUrl:inputs/input},Distance__value_or_field_:{paramUrl:inputs/Distance__value_or_field_}},messages:[]}3,
Note:
From 2, we can see that the URL number 2 is executed three times. Here, my personal understanding is that 2 requests sometimes fail to return results, so the request continues until the request is successful.
Key code:
var gpUrl = 'http://localhost:6080/arcgis/rest/services/erase/GPServer/erase';
Note:
This GP service calculates erase analysis without input, that is, the data parameters have been defined. It is two shp files and only outputs the calculation result as the output result.
1,
$ (# CalGP ). on (click, function () {$. messager. progress ({text: Computing in progress, please wait ...}); $. ajax ({type: POST, cache: false, url: gpUrl +/submitJob? F = json, async: true, success: function (result) {result = eval (+ result +); var jobId = result. jobId; if (jobId) {submitJob (jobId);} else {$. messager. SS ('close'); $. messager. alert (prompt, calculation failed !); }}});});Note:
After the url1 request is successful, a jobid is returned.
2,
function submitJob(jobId){ $.ajax({ type: POST, cache: false, url: gpUrl+/jobs/+jobId+?f=json, async: true, success: function (result1) { result1 = eval((+result1+)); if(result1.jobStatus===esriJobSucceeded){ showResult(jobId); } else{ submitJob(jobId); } } }); }
Note:
Request failed. Submit the request here until the request is successful.
3,
Function showResult (jobId) {$. ajax ({type: POST, cache: false, url: gpUrl +/jobs/+ jobId +/results/erase_shp? F = json & returnType = data, async: true, success: function (result2) {result2 = eval (+ result2 +); $. messager. SS ('close'); $. messager. alert (prompt, computation is complete !); Var features = result2.value. features; if (features. length> 0) {for (var I = 0, length = result2.length; I! = Length; ++ I) {var feature = features [I]; if (feature = undefined) {return;} else {var polySymbolRed = new esri. symbol. simpleFillSymbol (); polySymbolRed. setOutline (new esri. symbol. simpleLineSymbol (esri. symbol. simpleLineSymbol. STYLE_SOLID, new dojo. color ([0, 0, 0, 0.5]), 1); polySymbolRed. setColor (new dojo. color ([255, 0, 0, 0.5]); var g = new esri. graphic (new esri. geometry. polygon (feature. geometry), polySymbolRed, feature. attributes) map. graphics. add (g );}}}}});}