admin管理员组

文章数量:1392073

I'm using the wikipedia and Google APis.

When I select a city, I want to display the page from wikipedia. I have some problems on putting the city variable in wiki search url.

The city is in myVariable var.

    **var myVariable = locality;** 
      $.ajax({
          type: "GET",
          url: ".php?action=parse
&format=json&prop=text&section=0&page= + myVariable + &callback=?",

I know that in javascript we make the concatenation with + but I guess I'm doing something wrong there and I can't figure out what.

I'm using the wikipedia and Google APis.

When I select a city, I want to display the page from wikipedia. I have some problems on putting the city variable in wiki search url.

The city is in myVariable var.

    **var myVariable = locality;** 
      $.ajax({
          type: "GET",
          url: "http://en.wikipedia/w/api.php?action=parse
&format=json&prop=text&section=0&page= + myVariable + &callback=?",

I know that in javascript we make the concatenation with + but I guess I'm doing something wrong there and I can't figure out what.

Share Improve this question edited Feb 24, 2015 at 20:20 crixi asked Feb 24, 2015 at 20:19 crixicrixi 1592 gold badges4 silver badges12 bronze badges 3
  • 1 What do you think " + foo + " does? What about " ++++ foo ++++ "? – Felix Kling Commented Feb 24, 2015 at 20:22
  • 2 You could use the technique called "debugging". Instead of sitting around staring at your code and wondering why it doesn't work, or wasting other people's time, in "debugging" you actually do things yourself to figure out why it doesn't work. For instance, you could assign the url string you are creating to a variable, and then examine it in a debugger. If you don't know what that is, find out right away before you code another line. This has the advantage that you can actually solve your own problems instead of having to post to SO every time. – user663031 Commented Feb 24, 2015 at 20:25
  • 1 ^ developer.mozilla/en-US/docs/Debugging_JavaScript – Felix Kling Commented Feb 24, 2015 at 20:25
Add a ment  | 

2 Answers 2

Reset to default 2

Try this.

url: "http://en.wikipedia/w/api.php?action=parse&format=json&prop=text&section=0&page=" + myVariable + "&callback=?"

Do this...

url: "http://en.wikipedia/w/api.php?action=parse
&format=json&prop=text&section=0&page=" + myVariable + "&callback=?"

本文标签: javascriptConcatenate a variable in a urlStack Overflow